Method called in creator is called from base class but not from derived class

joe_specimen

Maybe this was covered in some other topic. But I did not find any satisfactory answer. Could somebody explain me following. I have following code:

#include <iostream>

class Base {
public:
   Base() {
      foo();
   }

   virtual void foo() { 
      std::cout << "Base::foo()" << std::endl; 
   }
};

class Derived : public Base {
public:
   Derived(): Base() {}

   virtual void foo() { 
      std::cout << "Derived::foo()" << std::endl; 
   }
};

int main() {
   Derived* p = new Derived();
}

Now my question is why does the Base creator calls foo method which is in the Base and not in the Derived class, although it is overridden in the Derived class?

LmTinyToon

It is bad pratice to call virtual functions inside of constructor. I just give simple example, which will show disadvantages of this approach (Note: It is about c++ (other languages can use different implementation).

Order of calling constructors in your code is:

1) Base

2) Derived

At the moment of calling Base constructor, member specified in Derived class is not created. Suppose, you can use function, overrided in Derived class. In this case you can invoke functions, which will have access to NOT CREATED DATA (note, at the moment Derived class is not created). Obviously, it is too dangerous. At that point it seems logicaly to call function, which can work with created data (in this case it is data of Base class)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Base Class __init__ being called automatically from derived class

Preventing a static method in base class from being called through derived class?

Will GetType() return the most derived type when called from the base class?

Why `this` can't access the derived class members from base class methods when called for derived class object

Error "__init__ method from base class is not called" for an abstract class

Is it possible a base class gets informed when a method in derived class is called?

Pass a pointer or reference to derived class such that base class method is called

Clone derived class from base class method

How to hint base class function that returns an instance of derived class it is called from?

Return derived type from base class method

Virtual Destructor Not called in Base as well as Derived Class

Binding derived class method in C++ to be called from separated std::function variable, outside the scope of the instance

How can I assure a pure virtual method is called from my derived class?

Can a class method be called from an async pipe?

How to get the class from which a method was called?

Inheritance: Is there a way to discover the class a method was called from?

Make method can be called from class or instance

Dynamically call a method from a dynamically called class

Class Scope with method called from subclass JS

Infer 'this' pointer type when called from derived class?

Derived Class Function Not Called

is it possible to allow an abstract method to be implemented in derived class, but only called in the base class?

Derived class not inheriting overloaded method from base class

Calling derived class method from base class destructor

using derived class data members from base class method

Call method of base class from derived class object python

Call derived class method from base class instance

Calling a method from a derived class object that returns a base class pointer

Method for any class derived from a generic base class