🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #2462

Re: Why allow virtual function calls in co

#2462
From: Fergus Henderson
Date: Tue, 07 Nov 1995 18:29
50 lines
2213 bytes
clamage@Eng.Sun.COM (Steve Clamage) writes:

>vandevod@cs.rpi.edu (David Vandevoorde) writes:
>>
>>SC> I think you would have to add this feature, or else disallow
>>SC> virtual calls from constructors and destructors.
>>
>>Right now, I think it makes sense disallowing them.
>
>The problem is actually a little more subtle. You also have to disallow
>passing the value of "this" out of a constructor or destructor, or 
>make the results of doing so undefined. A constructor (destructor) for T
>might call an ordinary global function which takes a pointer to T and
>calls a virtual function.

Even disallowing passing the value of `this' out of a ctor or dtor
would not be sufficient, because a ctor or dtor might call an ordinary
global function which gets a pointer which happens to be the same as
`this' by using the address of a global or static variable:

	struct Foo {
		Foo();
		virtual void virtual_func();
	}

	Foo x;

	void bar() {
		x.virtual_func();
	}

	Foo::Foo() {
		bar();
	}

On the other hand, disallowing passing the value of `this' out of
a ctor or dtor is not necessary either - you could simply say that
any direct *or indirect* call to a virtual function from within
a ctor or dtor gives undefined behaviour.  Mind you, I don't
think this would make things easier for programmers, and I wouldn't
advocate it.

--
Fergus Henderson             	WWW: http://www.cs.mu.oz.au/~fjh
fjh@cs.mu.oz.au              	PGP: finger fjh@128.250.37.3

---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID: <9531201.21274@mulga.cs.mu.OZ.AU>
Path: arc1-ca.pugleaf.net!128.32.155.1!matt
References: <VANDEVOD.95Oct29144834@avs.cs.rpi.edu> <472ujv$ns0@engnews1.eng.sun.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!128.32.155.1!matt
From: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
Newsgroups: comp.std.c++
Subject: Re: Why allow virtual function calls in co
Date: 07 Nov 1995 18:29:28 GMT
Organization: Computer Science, University of Melbourne, Australia
Approved: matt@physics2.Berkeley.EDU (Matt Austern)
Message-ID: <9531201.21274@mulga.cs.mu.OZ.AU>
References: <VANDEVOD.95Oct29144834@avs.cs.rpi.edu> <472ujv$ns0@engnews1.eng.sun.com>
NNTP-Posting-Host: physics2.berkeley.edu