Defect report , cannot call base class functions from constructor, was Re: How to do this??? (function pointer to other class)
[I've cross posted to comp.std.c++, because IMHO, this is a standards
problem, probably a defect. I'd set follow-ups as well, but I'm posting
through Google, which doesn't allow it.]
In practice, and I think in intent, you are right. However, the
standard makes some pretty stringent restrictions in 3.8. To start
with, it says (in paragraph 1):
The lifetime of an object is a runtime property of the object. The
lifetime of an object of type T begins when:
-- storage with the proper alignment and size for type T is
obtained, and
-- if T is a class type with a non-trivial constructor, the
constructor calls has COMPLETED.
The lifetime of an object of type T ends when:
-- if T is a class type with a non-trivial destructor, the
destructor call STARTS, or
-- the storage which the object occupies is reused or released.
(Emphisis added.) Then when we get down to paragraph 5, it says:
Before the lifetime of an object has started but after the storage
which the object will occupy has been allocated [which sounds to me
like it would include in the constructor, given the text above] or,
after the lifetime of an object has ended and before the storage
which the object occupied is reused or released, any pointer that
refers to the storage location where the object will be or was
located may be used but only in limited ways. [...] If the object
will be or was of a non-POD class type, the program has undefined
behavior if:
[...]
-- the pointer is implicitly converted to a pointer to a base class
type, or [...]
I can't find any exceptions for the this pointer.
Note that calling a non-static function in the base class, or even
constructing the base class in initializer list, involves an implicit
conversion of this to a pointer to the base class. Thus undefined
behavior. I'm sure that this wasn't the intent, but it would seem to be
what this paragraph is saying.
--
James Kanze GABI Software mailto:kanze@gabi-soft.fr
Conseils en informatique orientée objet/ http://www.gabi-soft.fr
Beratung in objektorientierter Datenverarbeitung
11 rue de Rambouillet, 78460 Chevreuse, France, +33 (0)1 30 23 45 16
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
------------ And now a word from our sponsor ---------------------
For a secure high performance FTP using SSL/TLS encryption
upgrade to SurgeFTP
---- See http://netwinsite.com/sponsor/sponsor_surgeftp.htm ----
|