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

Girardi

I have a base class and two derived classes. The base class constructor should calculate some properties when it is called, although those properties depend on the derived class details. To avoid recoding the same steps inside each derived class constructor, I code these steps in the base class constructor like in the example below.

The problem is that when I do this, the base class constructor do not call the overridden methods. Instead, it calls its own method. Is there a way to solve that? Although it makes sense as well, what's the reason this behavior happens?

Coming from a C# background, that's very odd, since it would work nicely there. In C#, I would use the keyword base to call any base class method whereas this would always call the derived class methods.

Example:

example.h

#define _USE_MATH_DEFINES
#include <math.h>

class Base
{
    public:
        Base(void);
        ~Base(void);
    protected:
        virtual void Method(void);
};


class Derived : public Base
{
    public:
        Derived(void);
        ~Derived(void);
    protected:
        virtual void Method(void);
};

example.cpp

#include <iostream>

Base::Base()
{
    this->Method(); // This calls Base->Method instead of Derived->Method
}

Base::~Base(){}

void Base::Method() // If I remove this, I have an error "externals undefined"
{
    std::cout << "called Base->Method()" << endl;
}

Derived::Derived()
    : Base()
{
    this->Method(); // This obviously calls Derived->Method
}

Derived::~Derived(){}

void Derived::Method()
{
    std::cout << "called Derived->Method()" << endl;
}

main.cpp

int main()
{
    Base* d = new Derived();

    /*
    Outputs:
        called Base->Method()
        called Derived->Method()
    */
}
seldon

There is no way to do that, because when the base class constructor is run, the object is not yet an object of the derived type. In particular, data members introduced in the derived class are not initialised until after the base class constructor is run -- in essence, the base class object behaves like a data member of the derived class.

One way or another, you will have to defer the calculation until the derived class's constructor is entered. If I understand what you're trying to do correctly, the best way is probably to give the base class a member function that does the calculations and call that from the derived classes' constructors.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I call an auxiliary base-class constructor from a derived-class auxiliary constructor in Scala?

From Dev

How do I call an auxiliary base-class constructor from a derived-class auxiliary constructor in Scala?

From Dev

How do I call a base class method from within the same overloaded derived class method in python?

From Dev

How to call derived class method from base class pointer?

From Dev

How do I access an inner class constructor from a derived class?

From Dev

Can I call a derived method from base class?

From Dev

C++ how to call Base class method from multiple derived class?

From Dev

Base Constructor Call in Derived Class

From Dev

Can you add a Derived Class to a list of its base class then call a method of the Derived class from the list of base class in C#

From Dev

Can you add a Derived Class to a list of its base class then call a method of the Derived class from the list of base class in C#

From Dev

How to call constructor of a template base class in a template derived class?

From Dev

Call derived class method from base class instance

From Dev

Call derived class method from base class instance

From Dev

How do I call the method of a derived class from a list that contains parent objects?

From Dev

Why can I call base template class method from derived class

From Dev

Derived class does not call base class method

From Dev

C++ Cannot call base class method from within derived class

From Dev

Call a method of a derived class from an array of pointers of type base class. (C++)

From Dev

How do I call a constructor within a method in the same class?

From Dev

How to call an operator from a base class within the derived class?

From Dev

How to call a function from a derived class in a base class?

From Dev

In python, how do I call the correct constructor from the method of a mixin class?

From Dev

How do I call a base class's own method from another method when it's overridden?

From Dev

Constructor in base and derived class

From Dev

How can I call the constructor of the derived class when using CRTP?

From Dev

How do I call a method on a specific base class in Python?

From Dev

Clone derived class from base class method

From Dev

Base class calls method from derived class?

From Dev

Clone derived class from base class method

Related Related

  1. 1

    How do I call an auxiliary base-class constructor from a derived-class auxiliary constructor in Scala?

  2. 2

    How do I call an auxiliary base-class constructor from a derived-class auxiliary constructor in Scala?

  3. 3

    How do I call a base class method from within the same overloaded derived class method in python?

  4. 4

    How to call derived class method from base class pointer?

  5. 5

    How do I access an inner class constructor from a derived class?

  6. 6

    Can I call a derived method from base class?

  7. 7

    C++ how to call Base class method from multiple derived class?

  8. 8

    Base Constructor Call in Derived Class

  9. 9

    Can you add a Derived Class to a list of its base class then call a method of the Derived class from the list of base class in C#

  10. 10

    Can you add a Derived Class to a list of its base class then call a method of the Derived class from the list of base class in C#

  11. 11

    How to call constructor of a template base class in a template derived class?

  12. 12

    Call derived class method from base class instance

  13. 13

    Call derived class method from base class instance

  14. 14

    How do I call the method of a derived class from a list that contains parent objects?

  15. 15

    Why can I call base template class method from derived class

  16. 16

    Derived class does not call base class method

  17. 17

    C++ Cannot call base class method from within derived class

  18. 18

    Call a method of a derived class from an array of pointers of type base class. (C++)

  19. 19

    How do I call a constructor within a method in the same class?

  20. 20

    How to call an operator from a base class within the derived class?

  21. 21

    How to call a function from a derived class in a base class?

  22. 22

    In python, how do I call the correct constructor from the method of a mixin class?

  23. 23

    How do I call a base class's own method from another method when it's overridden?

  24. 24

    Constructor in base and derived class

  25. 25

    How can I call the constructor of the derived class when using CRTP?

  26. 26

    How do I call a method on a specific base class in Python?

  27. 27

    Clone derived class from base class method

  28. 28

    Base class calls method from derived class?

  29. 29

    Clone derived class from base class method

HotTag

Archive