Kate
9/28/2008 8:59:00 PM
On Sun, 28 Sep 2008 10:15:50 -0500, Obnoxious User wrote:
> On Sun, 28 Sep 2008 14:15:50 +0000, The Doctor wrote:
>
>> 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
>
> I assume: class firstDerived : public Base
>
>> {
>> public:
>> virtual void theMagicalVeryUsefullFunction();
>> };
>>
>> void firstDerived::theMagicalVeryUsefullFunction() {
>> cout << "Called from the first derived class" << endl;
>> }
>>
>> class secondDerived
>
> I assume again: class secondDerived : public Base
>
>> {
>> 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?)
>
> You mean like:
>
> void firstDerived::theMagicalVeryUsefullFunction() {
> Base::theMagicalVeryUsefullFunction(); cout << "Called from the
first
> derived class" << endl;
> }
>
> void secondDerived::theMagicalVeryUsefullFunction() {
> firstDerived::theMagicalVeryUsefullFunction(); cout << "Called
from the
> secondDerived class" << endl;
> }
Thanks, that helped me out. By the way I forgot the public stuff ;)