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 1068
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_in_English
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_in_English
On 28 Sep, 15:15, The Doctor <do....@email.mewrote:
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.
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 ;) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Sat |
last post by:
Hi,
I have a simplified version of a problem that I am facing (hope I haven't
oversimplified it). This code doesn't work now and I want to find how I can
make it work. Can I call the derived...
|
by: Daniel Graifer |
last post by:
Why doesn't c++ support virtual data? It supports class data of type
pointer to function (that's what virtual functions really are). In
terms of implementation, why can't I have other types of...
|
by: santosh |
last post by:
Hello,
I was going through the Marshal Cline's C++ FAQ-Lite.
I have a doubt regarding section 33.10.
Here he is declaring a pure virtual destructor in the base class.
And again defining...
|
by: Mike Stevenson |
last post by:
Hi. I'm in the process of re-learning all the C++ I forgot from
college, and I'm starting to get into some virgin (or at least only a
couple times) territory. I have some questions about casting...
|
by: John Goche |
last post by:
Hello,
page 202 of Symbian OS Explained by Jo Stichbury states
"All virtual functions, public, protected or private, should be
exported"
then page 203 states
"In the rare cases where a...
|
by: v4vijayakumar |
last post by:
Why we need "virtual private member functions"? Why it is not an
(compile time) error?
|
by: Jess |
last post by:
Hello,
If I have a class that has virtual but non-pure declarations, like
class A{
virtual void f();
};
Then is A still an abstract class? Do I have to have "virtual void
f() = 0;"...
|
by: cmonthenet |
last post by:
Hello,
I searched for an answer to my question and found similar posts,
but none that quite addressed the issue I am trying to resolve.
Essentially, it seems like I need something like a virtual...
|
by: Chris Gordon-Smith |
last post by:
Hello All
I have a base class called Action_Request, and a set of classes
corresponding to different kinds of Action_Request, each of which inherits
from Action_Request. Eg:-
class ...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| | |