T
The Doctor
I have a question about virtual functions, and all that stuff.
Let's say, I have three classes.
class Base;
class firstDerived;
class secondDerived;
I defined them as following:
#include <iostream>
using namespace std;
class Base
{
public:
virtual void theMagicalVeryUsefullFunction();
};
void Base::theMagicalVeryUsefullFunction()
{
cout << "Called from the base class" << endl;
}
class firstDerived
{
public:
virtual void theMagicalVeryUsefullFunction();
};
void firstDerived::theMagicalVeryUsefullFunction()
{
cout << "Called from the first derived class" << endl;
}
class secondDerived
{
public:
virtual void theMagicalVeryUsefullFunction();
};
void secondDerived::theMagicalVeryUsefullFunction()
{
cout << "Called from the secondDerived class" << endl;
}
int main(int argc, char* argv[])
{
secondDerived* theObject = new secondDerived;
((Base*)theObject)->theMagicalVeryUsefullFunction();
}
This program doesn't exactly do what I want it to do, which is completely
normal. What I want it to do, is call ALL of the
theMagicalVeryUsefullFunction()'s, including the one from the Base, the
firstDerived and at last the secondDerived's one. Is that possible,
without a ((secondDerived*)this)->theMagicalVeryUsefullFunction() call in
the base class (or so?)
Let's say, I have three classes.
class Base;
class firstDerived;
class secondDerived;
I defined them as following:
#include <iostream>
using namespace std;
class Base
{
public:
virtual void theMagicalVeryUsefullFunction();
};
void Base::theMagicalVeryUsefullFunction()
{
cout << "Called from the base class" << endl;
}
class firstDerived
{
public:
virtual void theMagicalVeryUsefullFunction();
};
void firstDerived::theMagicalVeryUsefullFunction()
{
cout << "Called from the first derived class" << endl;
}
class secondDerived
{
public:
virtual void theMagicalVeryUsefullFunction();
};
void secondDerived::theMagicalVeryUsefullFunction()
{
cout << "Called from the secondDerived class" << endl;
}
int main(int argc, char* argv[])
{
secondDerived* theObject = new secondDerived;
((Base*)theObject)->theMagicalVeryUsefullFunction();
}
This program doesn't exactly do what I want it to do, which is completely
normal. What I want it to do, is call ALL of the
theMagicalVeryUsefullFunction()'s, including the one from the Base, the
firstDerived and at last the secondDerived's one. Is that possible,
without a ((secondDerived*)this)->theMagicalVeryUsefullFunction() call in
the base class (or so?)