[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

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

Kate

9/28/2008 2:16:00 PM

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

Obnoxious User

9/28/2008 3:16:00 PM

0

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

--
OU
Remember 18th of June 2008, Democracy died that afternoon.
http://frapedia.se/wiki/Information_...

Obnoxious User

9/28/2008 3:20:00 PM

0

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
>

I meant: class secondDerived : public firstDerived

--
OU
Remember 18th of June 2008, Democracy died that afternoon.
http://frapedia.se/wiki/Information_...

gw7rib

9/28/2008 7:27:00 PM

0

On 28 Sep, 15:15, The Doctor <do....@email.me> wrote:
> 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.

Kate

9/28/2008 8:59:00 PM

0

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