This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
Re:g++:virtual function call failes in constructor
- To: gcc-bugs at gcc dot gnu dot org
- Subject: Re:g++:virtual function call failes in constructor
- From: Martin Reinecke <martin at MPA-Garching dot MPG dot DE>
- Date: Tue, 23 Jan 2001 14:05:55 +0100
- CC: weberm at physik dot rwth-aachen dot de
- Organization: Max-Planck-Institut fuer Astrophysik
Hi,
> The call to the virtual function Print() in A::A() is not resolved to
> B::Print() in the statement B b; as it is expected for a virtual function.
As far as I can tell, gcc does the right thing here.
The Print() method is called in the constructor of class A, which is called
during the construction of the variable b in your main program.
It is not possible to call a virtual function of a class object that has
not been fully constructed. Since an object of type B is only
fully constructed when the opening brace "{" of the constructor of B
is reached, your object is already of type A, but not yet of type B
at the moment where you call Print().
This is mentioned somewhere in the 3rd edition of Stroustrup: The C++
Programming Language, but I don't remember the exact location.
Martin