This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re:g++:virtual function call failes in constructor


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

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]