Can Base Call to Call Overridden Function?

Hi, I just wondering if a base class can call the overridden function from a Derived class?

Here's an example:

//Base Class H
class BaseClass
{
public:
BaseClass();
virtual ~BaseClass();

virtual void functionA();
}

//Derived Class H
class DerivedClass : public BaseClass
{
public:
DerivedClass();
virtual ~DerivedClass();

virtual void functionA(); // Overridden function
}

//Base Class CPP
BaseClass::BaseClass()
{
this->functionA();
}

BaseClass::~BaseClass()
{
}

void BaseClass::functionA()
{
printf("BaseClass");
}

//Derived Class CPP
Derived::Derived():BaseClass()
{
}

Derived::~Derived()
{
}

void Derived::functionA()
{
printf("Derived");
}

// In Main
void main()
{
Derived* newObj = new Derived();
}

So basically, when I am creating a new object of Derived class, it will initialize BaseClass and the BaseClass will call functionA but I want it to call the function overridden by Derived class.

I know that if I call newObj->functionA it will call the overridden function. Right now I want the base class to call the overridden function "this->functionA(); in BaseClass" during its initialization. Is it possible to do that?

I hope somebody can help me thanks.

-FaRHaN-
Last edited on
Base class knows nothing about its derived classes.
I think I know what you mean. It's not that Base calls the derived class. It's that a pointer to a base class could potentially call functions from a Derived class as long as the function exists as virtual in the base class.

Check this out:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Base
{
public:
    virtual void someFunc() { cout << "Base::someFunc" << endl; }
    void someOtherFunc() { cout << "Base::someOtherFunc" << endl;
};

class Derived : public Base
{
public: 
    void someFunc() { cout << "Derived::someFunc" << endl; }
};

int main()
{
    Base b* = new Derived();
    b->someFunc();             // Calls Derived::someFunc()
    b->someOtherFunc();    // Calls Base::someOtherFunc()
}


If Derived over-rides the virtual function, then the Derived one gets called. If Derived does not over-ride the base function, the you can use the base-function's implementation instead. It's super useful!
Last edited on
Right now I want the base class to call the overridden function "this->functionA(); in BaseClass" during its initialization. Is it possible to do that?


No. During base class initialization, no portion of the derived class (assuming one will even be constructed) exists.
I think you could using the solution I mention above:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Base
{
private:
    void functionA();
public:
    Base() { functionA(); }
};

class Derived : public Base {};

int main()
{
    Base b* = new Derived();
}


Here we are creating a Derived() class. During the construction, we are calling the Base constructor and functionA() in addition to a Derived constructor. We can call anything inside of Derived as long as Base provides an interface for it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>

struct base
{
    base() { foo() ; } // calls base::foo()
    virtual ~base() { foo() ; } // calls base::foo()

    virtual void foo() const { std::cout << "base::foo\n" ; }
    void bar() { foo() ; } // calls ???::foo
};

struct derived : base
{
    derived() { foo() ; } // calls derived::foo()
    virtual ~derived() override { foo() ; } // calls derived::foo()
    virtual void foo() const override { std::cout << "derived::foo\n" ; }
};

int main()
{
    {
        derived d ;
        // constructor of base class
        // base constructor calls foo => base::foo
        // constructor of derived class
        // derived constructor calls foo => derived::foo

        base& b = d ;
        b.bar() ;
        // calls base::bar()
        // base::bar calls foo() polymorphically => derived::foo

    } // the life-time of d is over at this point
      // destructor of derived class
      // derived destructor calls foo => derived::foo
      // destructor of base class
      // base destructor calls foo => base::foo
}

http://ideone.com/3bStA3
No. During base class initialization, no portion of the derived class (assuming one will even be constructed) exists.

Thanks cire, i guess i have to figure out a different way. I am actually converting and IOS (Objective C) game to Android (C++). It seems that Objective C works fine as a base class calling an overridden function during initialization.
C# has the same problem as all other languages:
http://ideone.com/PjNhjM

You should never try to call a function of the derived class in the base class constructor, as the derived class has not initialized its state yet. Whatever C# code you are reading is poorly written or is using a clever hacky solution ;p
Thanks everyone, I already solved the problem :)

Here's some more info regarding this issue.

http://www.parashift.com/c%2B%2B-faq-lite/calling-virtuals-from-ctors.html
http://www.parashift.com/c%2B%2B-faq-lite/calling-virtuals-from-dtors.html

"C++ is protecting you from yourself" LOL

Anyway what I did is, I moved out the call to the virtual function from the Constructor and add a new function called init() which I call from the Derived Class after the classes has been constructed.

Here's the new solution that works :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//Base Class H
class BaseClass
{
public:
BaseClass();
virtual ~BaseClass();
virtual void init();

virtual void functionA();
}

//Derived Class H
class DerivedClass : public BaseClass
{
public:
DerivedClass();
virtual ~DerivedClass();

virtual void functionA(); // Overridden function
}

//Base Class CPP
BaseClass::BaseClass()
{
}

BaseClass::~BaseClass()
{
}

virtual void init()
{
   this->functionA();
}

void BaseClass::functionA()
{
   printf("BaseClass");
}

//Derived Class CPP
Derived::Derived():BaseClass()
{
   BaseClass::init();
}

Derived::~Derived()
{
}

void Derived::functionA()
{
   printf("Derived");
}

// In Main
void main()
{
   Derived* newObj = new Derived();
}


Right now, this->functionA() from the BaseClass will use the function override by the Derived Class :) Yay!!!

-FaRHaN-
Last edited on
Init should be non-virtual and protected ;p
Topic archived. No new replies allowed.