C++: call overrided method in base class's constructor

dudu

Suppose I have a base class A and a derived class B. B's constructor calls the constructor of A, in which I call a function, say func to do some type-dependent thing. I mean, I have a do-nothing func for A and override this method in B.

My problem:
In the very phase of construction of B, that is, in the constructor of A, what is the type of the object? While, I think it's A. But I'm not sure. If it is A, I'm always calling A's func right? Regardless of the type of the object I want to construct.
In VS, in B's constructor, I see the type for "this" is "B". While I step into and in A's constructor, I see type for "this" "A".

Sam Varshavchik

This is correct. Superclasses are constructed first. Derived classes get constructed only after the superclasses are constructed. Until your superclass A is constructed, none of its virtual methods are overridden, and calling them will invoke A's virtual method. If they are pure and not defined, this results in undefined behavior.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

c# wont use an new overrided method in the base class

Overrided method calling from base class c++

Why child class constructor when initializing parent's part used overrided version of method?

force base class to use its own method and not overrided method

How can I call a method of derivitive class within base's class constructor using a pointer to function?

returning derived class from overrided method, declared as returning base copy

How do I call a derived class method from the base class constructor in C++?

C++ derived class constructor call base class constructor errors

How to call the base class method in C++?

C++ inherited methods call the base class's method instead of overloaded method

Constructor parameter is null when referred to in an overrided method

How can I decide to call an overrided method in child class from either parent or child class?

Base class constructor automatic call

When in inheritance, base constructor call virtual method, why the class is Dervied and call Dervied method?

How does one call a "move" '&&' constructor in a base class? in C++

Call base class constructor after member constructor

How to call a method from a parent class's base class

Javascript Class Constructor Call Method

Use base function if not overrided abscract class

call c# class method by reflection with constructor using dependency injection

Call a method of the inherited class from the base class. C++

No self parameter passed in overrided python class' method

inherit base class's constructor

Call another object's method in constructor C++

Subclass calling Base Class constructor then using instance method of Base Class in Subclass Arduino C++

How to catch a base class constructor's exception in C++?

c++ call constructor of extended class stored in base class variable without knowing extended class

Is it possible to call a derived class constructor from a base class constructor?

Letting implicit derived class constructor call base class constructor