//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 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:
virtualvoid 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!
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.
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.
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
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.