ChatGPT解决这个技术问题 Extra ChatGPT

Calling virtual functions inside constructors

Suppose I have two C++ classes:

class A
{
public:
  A() { fn(); }

  virtual void fn() { _n = 1; }
  int getn() { return _n; }

protected:
  int _n;
};

class B : public A
{
public:
  B() : A() {}

  virtual void fn() { _n = 2; }
};

If I write the following code:

int main()
{
  B b;
  int n = b.getn();
}

One might expect that n is set to 2.

It turns out that n is set to 1. Why?

I'm asking and answering my own question because I want to get the explanation for this bit of C++ esoterica into Stack Overflow. A version of this issue has struck our development team twice, so I'm guessing this info might be of use to someone out there. Please write out an answer if you can explain it in a different/better way...
I'm wondering why this got down voted? When I first learned C++ this really confused me. +1
What surprises me is the lack of a compiler warning. The compiler substitutes a call to the “function defined in the class of the current constructor” for what would in any other case be the “most overridden” function in a derived class. If the compiler said “substituting Base::foo() for call to virtual function foo() in constructor” then the programmer would be warned that the code will not do what they expected. That would be a lot more helpful than making a silent substitution, leading to mysterious behavior, lots of debugging, and eventually a trip to stackoverflow for enlightenment.
@CraigReynolds Not necessarily. There is no need for special compiler treatment of virtual calls inside constructors The base class constructor creates the vtable for the current class only, so at that point the compiler can just call the vitrual function via that vtable in exactly the same way as usual. But the vtable doesn't point to any function in any derived class yet. The vtable for the derived class is adjusted by the derived class's constructor after the base class constructor returns, which is how the override will work once the derived class is constructed.

C
Community

Calling virtual functions from a constructor or destructor is dangerous and should be avoided whenever possible. All C++ implementations should call the version of the function defined at the level of the hierarchy in the current constructor and no further.

The C++ FAQ Lite covers this in section 23.7 in pretty good detail. I suggest reading that (and the rest of the FAQ) for a followup.

Excerpt:

[...] In a constructor, the virtual call mechanism is disabled because overriding from derived classes hasn’t yet happened. Objects are constructed from the base up, “base before derived”. [...] Destruction is done “derived class before base class”, so virtual functions behave as in constructors: Only the local definitions are used – and no calls are made to overriding functions to avoid touching the (now destroyed) derived class part of the object.

EDIT Corrected Most to All (thanks litb)


Not most C++ implementations, but all C++ implementations have to call the current class's version. If some don't, then those have a bug :). I still agree with you that it's bad to call a virtual function from a base class - but semantics are precisely defined.
It's not dangerous, it's just non-virtual. In fact, if methods called from the constructor were called virtually, it would be dangerous because the method could access uninitialized members.
Why is calling virtual functions from destructor dangerous? Isn't the object still complete when destructor runs, and only destroyed after the destructor finishes?
−1 "is dangerous", no, it's dangerous in Java, where downcalls can happen; the C++ rules remove the danger through a pretty expensive mechanism.
In what way is calling a virtual function from a constructor "dangerous"? This is total nonsense.
D
David Rodríguez - dribeas

Calling a polymorphic function from a constructor is a recipe for disaster in most OO languages. Different languages will perform differently when this situation is encountered.

The basic problem is that in all languages the Base type(s) must be constructed previous to the Derived type. Now, the problem is what does it mean to call a polymorphic method from the constructor. What do you expect it to behave like? There are two approaches: call the method at the Base level (C++ style) or call the polymorphic method on an unconstructed object at the bottom of the hierarchy (Java way).

In C++ the Base class will build its version of the virtual method table prior to entering its own construction. At this point a call to the virtual method will end up calling the Base version of the method or producing a pure virtual method called in case it has no implementation at that level of the hierarchy. After the Base has been fully constructed, the compiler will start building the Derived class, and it will override the method pointers to point to the implementations in the next level of the hierarchy.

class Base {
public:
   Base() { f(); }
   virtual void f() { std::cout << "Base" << std::endl; } 
};
class Derived : public Base
{
public:
   Derived() : Base() {}
   virtual void f() { std::cout << "Derived" << std::endl; }
};
int main() {
   Derived d;
}
// outputs: "Base" as the vtable still points to Base::f() when Base::Base() is run

In Java, the compiler will build the virtual table equivalent at the very first step of construction, prior to entering the Base constructor or Derived constructor. The implications are different (and to my likings more dangerous). If the base class constructor calls a method that is overriden in the derived class the call will actually be handled at the derived level calling a method on an unconstructed object, yielding unexpected results. All attributes of the derived class that are initialized inside the constructor block are yet uninitialized, including 'final' attributes. Elements that have a default value defined at the class level will have that value.

public class Base {
   public Base() { polymorphic(); }
   public void polymorphic() { 
      System.out.println( "Base" );
   }
}
public class Derived extends Base
{
   final int x;
   public Derived( int value ) {
      x = value;
      polymorphic();
   }
   public void polymorphic() {
      System.out.println( "Derived: " + x ); 
   }
   public static void main( String args[] ) {
      Derived d = new Derived( 5 );
   }
}
// outputs: Derived 0
//          Derived 5
// ... so much for final attributes never changing :P

As you see, calling a polymorphic (virtual in C++ terminology) methods is a common source of errors. In C++, at least you have the guarantee that it will never call a method on a yet unconstructed object...


Good job explaining why the alternative is (also) are error-prone.
"If the base class constructor calls a method that is overriden in the derived class the call will actually be handled at the derived level calling a method on an unconstructed object..." How so if base is already initialized. There is no possiblity unless you explicilty call "init" before initializing other members.
An explanation! +1, superior answer imho
For me the problem is that there are so many restrictions in C++ classes that its incredible hard to achieve any good design. C++ dictates that "If it could be dangerous forbid it" even if its intuitive causing problems such as: "Why this intuitive behavior doesn't work" to happen all the time.
@VinGarcia What? C++ does not "forbid" anything in this case. The call is simply treated as a non-virtual call, to the method for the class whose constructor is currently executing. That is a logical consequence of the object construction timeline - not some draconian decision to stop you doing silly things. The fact that it coincidentally fulfills the latter purpose too is just a bonus to me.
M
Macke

The reason is that C++ objects are constructed like onions, from the inside out. Base classes are constructed before derived classes. So, before a B can be made, an A must be made. When A's constructor is called, it's not a B yet, so the virtual function table still has the entry for A's copy of fn().


C++ does not normally use the term "super class" - it prefers "base class".
That is the same in most OO languages: you cannot possibly build a derived object without the base part being already constructed.
@DavidRodríguez-dribeas other languages do actually do that. For example in Pascal, memory is allocated for the whole object first, but then only the most-derived constructor is invoked. A constructor must either contain an explicit call to its parent's constructor (which does not have to be the first action - it just has to be somewhere), or if it doesn't , it's as if the first line of the constructor made that call.
Thanks for the clarity and avoidance of details which doesnt go straight to the outcome
If the calling still uses the vptr(since the vptr is set to the current level as you metioned too) way or just statically calls the version of the current level.
C
Cheers and hth. - Alf

The C++ FAQ Lite Covers this pretty well:

Essentially, during the call to the base classes constructor, the object is not yet of the derived type and thus the base type's implementation of the virtual function is called and not the derived type's.


Clear, straightforward, simplest answer. It's still a feature I would love to see get some love. I hate having to write all these silly initializeObject() functions that the user is forced to call right after the construction, just bad form for a very common use case. I understand the difficulty though. C'est la vie.
@moodboom What "love" do you propose? Bear in mind that you can't just change how things currently work in-place, because that would horribly break reams of existing code. So, how would you do it instead? Not only what new syntax you would introduce to allow (actual, non-devirtualised) virtual calls in constructors - but also how you would somehow amend the models of object construction/lifetime so that those calls would have a complete object of the derived type on which to run. This'll be interesting.
@underscore_d I don't think any syntax changes would be needed. Maybe when creating an object, the compiler would add code to walk the vtable and look for this case and patch things then? I've never written a C++ compiler and I'm quite sure my initial comment to give this some "love" was naive and this will never happen. :-) A virtual initialize() function isn't a very painful workaround anyway, you just have to remember to call it after creating your object.
@underscore_d I just noticed your other comment below, explaining that the vtable isn't available in constructor, emphasizing again the difficulty here.
@moodboom I goofed when writing about the vtable not being available in the constructor. It is available, but the constructor only sees the vtable for its own class, because each derived constructor updates the instance's vptr to point at the vtable for the current derived type and no further. So, the current ctor sees a vtable that only has its own overrides, hence why it can't call any more-derived implementations of any virtual functions.
d
davidA

One solution to your problem is using factory methods to create your object.

Define a common base class for your class hierarchy containing a virtual method afterConstruction():

class Object
{
public:
  virtual void afterConstruction() {}
  // ...
};

Define a factory method:

template< class C >
C* factoryNew()
{
  C* pObject = new C();
  pObject->afterConstruction();

  return pObject;
}

Use it like this:

class MyClass : public Object 
{
public:
  virtual void afterConstruction()
  {
    // do something.
  }
  // ...
};

MyClass* pMyObject = factoryNew();

type need to specify for template function MyClass* pMyObject = factoryNew();
F
François Andrieux

Other answers have already explained why virtual function calls don't work as expected when called from a constructor. I'd like to instead propose another possible work around for getting polymorphic-like behavior from a base type's constructor.

By adding a template constructor to the base type such that the template argument is always deduced to be the derived type it's possible to be aware of the derived type's concrete type. From there, you can call static member functions for that derived type.

This solution does not allow non-static member functions to be called. While execution is in the base type's constructor, the derived type's constructor hasn't even had time to go through it's member initialization list. The derived type portion of the instance being created hasn't begun being initialized it. And since non-static member functions almost certainly interact with data members it would be unusual to want to call the derived type's non-static member functions from the base type's constructor.

Here is a sample implementation :

#include <iostream>
#include <string>

struct Base {
protected:
    template<class T>
    explicit Base(const T*) : class_name(T::Name())
    {
        std::cout << class_name << " created\n";
    }

public:
    Base() : class_name(Name())
    {
        std::cout << class_name << " created\n";
    }


    virtual ~Base() {
        std::cout << class_name << " destroyed\n";
    }

    static std::string Name() {
        return "Base";
    }

private:
    std::string class_name;
};


struct Derived : public Base
{   
    Derived() : Base(this) {} // `this` is used to allow Base::Base<T> to deduce T

    static std::string Name() {
        return "Derived";
    }
};

int main(int argc, const char *argv[]) {

    Derived{};  // Create and destroy a Derived
    Base{};     // Create and destroy a Base

    return 0;
}

This example should print

Derived created
Derived destroyed
Base created
Base destroyed

When a Derived is constructed, the Base constructor's behavior depends on the actual dynamic type of the object being constructed.


this approach is cannot be extended, what if we need another class that inherit Derived and provide its own Name impl. the CRTP solution posted by @stands2reason is actually the defacto solution
To the extent a virtual override in Derived touches its data members, that 3rd paragraph also illustrates the danger if calls to virtuals behaved as in OPs ‘might expect that..‘alternative.
m
msc

The C++ Standard (ISO/IEC 14882-2014) say's:

Member functions, including virtual functions (10.3), can be called during construction or destruction (12.6.2). When a virtual function is called directly or indirectly from a constructor or from a destructor, including during the construction or destruction of the class’s non-static data members, and the object to which the call applies is the object (call it x) under construction or destruction, the function called is the final overrider in the constructor’s or destructor’s class and not one overriding it in a more-derived class. If the virtual function call uses an explicit class member access (5.2.5) and the object expression refers to the complete object of x or one of that object’s base class subobjects but not x or one of its base class subobjects, the behavior is undefined.

So, Don't invoke virtual functions from constructors or destructors that attempts to call into the object under construction or destruction, Because the order of construction starts from base to derived and the order of destructors starts from derived to base class.

So, attempting to call a derived class function from a base class under construction is dangerous.Similarly, an object is destroyed in reverse order from construction, so attempting to call a function in a more derived class from a destructor may access resources that have already been released.


s
stands2reason

As has been pointed out, the objects are created base-down upon construction. When the base object is being constructed, the derived object does not exist yet, so a virtual function override cannot work.

However, this can be solved with polymorphic getters that use static polymorphism instead of virtual functions if your getters return constants, or otherwise can be expressed in a static member function, This example uses CRTP (https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern).

template<typename DerivedClass>
class Base
{
public:
    inline Base() :
    foo(DerivedClass::getFoo())
    {}

    inline int fooSq() {
        return foo * foo;
    }

    const int foo;
};

class A : public Base<A>
{
public:
    inline static int getFoo() { return 1; }
};

class B : public Base<B>
{
public:
    inline static int getFoo() { return 2; }
};

class C : public Base<C>
{
public:
    inline static int getFoo() { return 3; }
};

int main()
{
    A a;
    B b;
    C c;

    std::cout << a.fooSq() << ", " << b.fooSq() << ", " << c.fooSq() << std::endl;

    return 0;
}

With the use of static polymorphism, the base class knows which class' getter to call as the information is provided at compile-time.


I think I will avoid to do this. This is not single Base class any more. You actually created lots of different Base class.
@Wang Exactly: Base<T> is just a helper class, not a common interface type that can be used for runtime polymorphism (f.ex. heterogeneous containers). These are useful too, just not for the same tasks. Some classes inherit both from a base class that's an interface type for runtime polymorphism and another that's a compile time template helper.
T
TimW

Do you know the crash error from Windows explorer?! "Pure virtual function call ..." Same problem ...

class AbstractClass 
{
public:
    AbstractClass( ){
        //if you call pureVitualFunction I will crash...
    }
    virtual void pureVitualFunction() = 0;
};

Because there is no implemetation for the function pureVitualFunction() and the function is called in the constructor the program will crash.


It's hard to see how this is the same problem, as you didn't explain why. Calls to non-pure virtual functions during ctors are perfectly legal, but they just don't go through the (not yet constructed) virtual table, so the version of the method that gets executed is the one defined for the class type whose ctor we are in. So those don't crash. This one does because it's pure virtual and unimplemented (side note: one can implement pure virtual functions in the base), so there is no version of the method to be called for this class type, & the compiler assumes you don't write bad code, so boom
D'oh. The calls do go through the vtable, but it hasn't yet been updated to point at the overrides for the most-derived class: only the one being constructed right now. Still, the result and reason for the crash remains the same.
@underscore_d "(side note: one can implement pure virtual functions in the base)" No you can not, otherwise the method is no longer pure virtual. You also can not make instances of an abstract class, so the example by TimW will not compile if you try to call a pure method from the constructor. It now compiles because the constructor does not call pure virtual methods and contains no code, just a comment.
Y
Yogesh

The vtables are created by the compiler. A class object has a pointer to its vtable. When it starts life, that vtable pointer points to the vtable of the base class. At the end of the constructor code, the compiler generates code to re-point the vtable pointer to the actual vtable for the class. This ensures that constructor code that calls virtual functions calls the base class implementations of those functions, not the override in the class.


The vptr isn't changed at the end of the ctor. In the body of ctor C::C, virtual function calls go the C overrider, not to any base class version.
The dynamic type of the object is defined after the ctor has called base class ctors and before it constructs its members. So the vptr isn't changed at the end of the ctor.
@curiousguy I am saying the same thing, that vptr is not changed at the end of constructor of base class, it will be changed at the end of constructor of derived class. I hope you are telling the same. Its an compiler/implementation dependent thing. When are you proposing that vptr should change. Any good reason for downvoting?
The timing of the change of vptr is not implementation dependent. It is prescribed by language semantics: the vptr changes when the dynamic behavior of the class instance changes. There is no freedom here. Inside the body of a ctor T::T(params), the dynamic type is T. The vptr will reflect that: it will point to vtable for T. Do you disagree?
Maybe it would easier to have a real example of inheritance to talk about
P
Priya

Firstly,Object is created and then we assign it 's address to pointers.Constructors are called at the time of object creation and used to initializ the value of data members. Pointer to object comes into scenario after object creation. Thats why, C++ do not allows us to make constructors as virtual . .another reason is that, There is nothing like pointer to constructor , which can point to virtual constructor,because one of the property of virtual function is that it can be used by pointers only.

Virtual functions are used to assign value dynamically,as constructors are static,so we can not make them virtual.


k
keyou

As a supplement, calling a virtual function of an object that has not yet completed construction will face the same problem.

For example, start a new thread in the constructor of an object, and pass the object to the new thread, if the new thread calling the virtual function of that object before the object completed construction will cause unexpected result.

For example:

#include <thread>
#include <string>
#include <iostream>
#include <chrono>

class Base
{
public:
  Base()
  {
    std::thread worker([this] {
      // This will print "Base" rather than "Sub".
      this->Print();
    });
    worker.detach();
    // Try comment out this code to see different output.
    std::this_thread::sleep_for(std::chrono::seconds(1));
  }
  virtual void Print()
  {
    std::cout << "Base" << std::endl;
  }
};

class Sub : public Base
{
public:
  void Print() override
  {
    std::cout << "Sub" << std::endl;
  }
};

int main()
{
  Sub sub;
  sub.Print();
  getchar();
  return 0;
}

This will output:

Base
Sub

Hello and welcome to SO! Please read the tour, and How do I write a good answer? For example adding a code snippet might help.
This solution has undefined behavior. sleep_for does not synchronize threads, so you have a race on this->Print() both during construction and destruction. Second, this risks crashing as the worker requires this to still exist (it is a member function) but there is no guarantee of that. If you don't have an arbitrary wait like getchar() the Sub instance can easily reach the end of its lifetime before the thread prints. Solutions that rely on detach() are almost always broken.
D
Don Slowik

To answer what happens/why when you run that code, I compiled it via g++ -ggdb main.cc, and stepped through with gdb.

main.cc:

class A { 
  public:
    A() {
      fn();
    }
    virtual void fn() { _n=1; }
    int getn() { return _n; }

  protected:
    int _n;
};


class B: public A {
  public:
    B() {
      // fn();
    }
    void fn() override {
      _n = 2;
    }
};


int main() {
  B b;
}

Setting a break point at main, then stepping into B(), printing the this ptr, taking a step into A() (base constructor):

(gdb) step
B::B (this=0x7fffffffde80) at main2.cc:16
16    B() {
(gdb) p this
$27 = (B * const) 0x7fffffffde80
(gdb) p *this
$28 = {<A> = {_vptr.A = 0x7fffffffdf80, _n = 0}, <No data fields>}
(gdb) s
A::A (this=0x7fffffffde80) at main2.cc:3
3     A() {
(gdb) p this
$29 = (A * const) 0x7fffffffde80

shows that this initially points at the derived B obj b being constructed on the stack at 0x7fffffffde80. The next step is into the base A() ctor and this becomes A * const to the same address, which makes sense as the base A is right in the start of B object. but it still hasn't been constructed:

(gdb) p *this
$30 = {_vptr.A = 0x7fffffffdf80, _n = 0}

One more step:

(gdb) s
4       fn();
(gdb) p *this
$31 = {_vptr.A = 0x402038 <vtable for A+16>, _n = 0}

_n has been initialized, and it's virtual function table pointer contains the address of virtual void A::fn():

(gdb) p fn
$32 = {void (A * const)} 0x40114a <A::fn()>
(gdb) x/1a 0x402038
0x402038 <_ZTV1A+16>:   0x40114a <_ZN1A2fnEv>

So it makes perfect sense that the next step executes A::fn() via this->fn() given the active this and _vptr.A. Another step and we're back in B() ctor:

(gdb) s
B::B (this=0x7fffffffde80) at main2.cc:18
18    }
(gdb) p this
$34 = (B * const) 0x7fffffffde80
(gdb) p *this
$35 = {<A> = {_vptr.A = 0x402020 <vtable for B+16>, _n = 1}, <No data     fields>}

The base A has been constructed. Note that address stored in the virtual function table pointer has changed to the vtable for derived class B. And so a call to fn() would select the derived class override B::fn() via this->fn() given the active this and _vptr.A (un-comment call to B::fn() in B() to see this.) Again examining 1 address stored in _vptr.A shows it now points to the derived class override:

(gdb) p fn
$36 = {void (B * const)} 0x401188 <B::fn()>
(gdb) x/1a 0x402020
0x402020 <_ZTV1B+16>:   0x401188 <_ZN1B2fnEv>

By looking at this example, and by looking at one with a 3 level inheritance, it appears that as the compiler descends to construct the base sub-objects, the type of this* and the corresponding address in _vptr.A change to reflect the current sub-object being constructed, - so it gets left pointing to the most derived type's. So we would expect virtual functions called from within ctors to choose the function for that level, i.e., same result as if they were non-virtual.. Likewise for dtors but in reverse. And this becomes a ptr to member while members are being constructed so they also properly call any virtual functions that are defined for them.


u
user2305329

I am not seeing the importance of the virtual key word here. b is a static-typed variable, and its type is determined by compiler at compile time. The function calls would not reference the vtable. When b is constructed, its parent class's constructor is called, which is why the value of _n is set to 1.


The question is why b's constructor calls the base f(), not the derived override of it. Type of the variable b is irrelevant to that.
"The function calls would not reference the vtable" That is not true. If you think virtual dispatch is only enabled when accessing through a B* or ` B&`, you are mistaken.
Aside from the fact that it follows its own logic to the wrong conclusion... The idea behind this answer, known static type, is misapplied. A compiler could devirtualise b.getN() because it knows the real type, & just directly dispatch to the version from B. But that's just an allowance made by the as-if rule. Everything still must act as-if the virtual table is used & followed to the letter. In the A constructor, the same is true: even if (probably not possible) it gets inlined w/ the B ctor, the virtual call must still act as-if it only has the base A vtable available to use.
@LightnessRacesinOrbit Can you give me an example for your assertion that virtual dispatch happens without calling through a reference or pointer (including the implicit this)?
@user2305329 You are right that the call b.getn() is non-virtual. b is a statically typed object, and whatever getn() is defined for its type will be called. But inside member functions, including the constructor, all member function calls are made through the implicit this pointer and are hence virtual function calls, if it is a polymorphic class. The reason and rationale for resolving the virtual fn() call to the base class's implementation -- even though it happens during the overall construction of a derived object -- is explained in the other answers.
t
terry

During the object's constructor call the virtual function pointer table is not completely built. Doing this will usually not give you the behavior you expect. Calling a virtual function in this situation may work but is not guaranteed and should be avoided to be portable and follow the C++ standard.


"Calling a virtual function in this situation may work but is not guaranteed" That is not correct. The behaviour is guaranteed.
@curiousguy ...guaranteed to call the base version if available, or to invoke UB if the vfunc is pure virtual.