Virtual functions, call all of them, not only the last derived.

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?)
 
G

gw7rib

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?)

Other than what Obnoxious User has suggested, is there any way you
could rig things so that theMagicalVeryUsefullFunction is in fact the
constructor or the destructor? These get called all the way down.

Without seeing your code, I can't tell whether this has the remotest
chance of working, but it might (perhaps!) be a possibility.
 
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

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 ;)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top