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 theMagicalVeryU sefullFunction( );
};
void Base::theMagica lVeryUsefullFun ction()
{
cout << "Called from the base class" << endl;
}
class firstDerived
{
public:
virtual void theMagicalVeryU sefullFunction( );
};
void firstDerived::t heMagicalVeryUs efullFunction()
{
cout << "Called from the first derived class" << endl;
}
class secondDerived
{
public:
virtual void theMagicalVeryU sefullFunction( );
};
void secondDerived:: theMagicalVeryU sefullFunction( )
{
cout << "Called from the secondDerived class" << endl;
}
int main(int argc, char* argv[])
{
secondDerived* theObject = new secondDerived;
((Base*)theObje ct)->theMagicalVery UsefullFunction ();
}
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
theMagicalVeryU sefullFunction( )'s, including the one from the Base, the
firstDerived and at last the secondDerived's one. Is that possible,
without a ((secondDerived *)this)->theMagicalVery UsefullFunction () call in
the base class (or so?) 4 1089
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 theMagicalVeryU sefullFunction( );
};
void Base::theMagica lVeryUsefullFun ction() {
cout << "Called from the base class" << endl;
}
class firstDerived
I assume: class firstDerived : public Base
{
public:
virtual void theMagicalVeryU sefullFunction( );
};
void firstDerived::t heMagicalVeryUs efullFunction() {
cout << "Called from the first derived class" << endl;
}
class secondDerived
I assume again: class secondDerived : public Base
{
public:
virtual void theMagicalVeryU sefullFunction( );
};
void secondDerived:: theMagicalVeryU sefullFunction( ) {
cout << "Called from the secondDerived class" << endl;
}
int main(int argc, char* argv[])
{
secondDerived* theObject = new secondDerived;
((Base*)theObje ct)->theMagicalVery UsefullFunction ();
}
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
theMagicalVeryU sefullFunction( )'s, including the one from the Base, the
firstDerived and at last the secondDerived's one. Is that possible,
without a ((secondDerived *)this)->theMagicalVery UsefullFunction () call
in the base class (or so?)
You mean like:
void firstDerived::t heMagicalVeryUs efullFunction()
{
Base::theMagica lVeryUsefullFun ction();
cout << "Called from the first derived class" << endl;
}
void secondDerived:: theMagicalVeryU sefullFunction( )
{
firstDerived::t heMagicalVeryUs efullFunction() ;
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 theMagicalVeryU sefullFunction( ); };
void Base::theMagica lVeryUsefullFun ction() { cout << "Called from the base class" << endl; }
class firstDerived
I assume: class firstDerived : public Base
>{ public: virtual void theMagicalVeryU sefullFunction( ); };
void firstDerived::t heMagicalVeryUs efullFunction() { 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.m ewrote:
What I want it to do, is call ALL of the
theMagicalVeryU sefullFunction( )'s, including the one from the Base, the
firstDerived and at last the secondDerived's one. Is that possible,
without a ((secondDerived *)this)->theMagicalVery UsefullFunction () call in
the base class (or so?)
Other than what Obnoxious User has suggested, is there any way you
could rig things so that theMagicalVeryU sefullFunction 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 theMagicalVeryU sefullFunction( ); };
void Base::theMagica lVeryUsefullFun ction() { cout << "Called from the base class" << endl; }
class firstDerived
I assume: class firstDerived : public Base
>{ public: virtual void theMagicalVeryU sefullFunction( ); };
void firstDerived::t heMagicalVeryUs efullFunction() { cout << "Called from the first derived class" << endl; }
class secondDerived
I assume again: class secondDerived : public Base
>{ public: virtual void theMagicalVeryU sefullFunction( ); };
void secondDerived:: theMagicalVeryU sefullFunction( ) { cout << "Called from the secondDerived class" << endl; }
int main(int argc, char* argv[]) { secondDerived* theObject = new secondDerived; ((Base*)theObje ct)->theMagicalVery UsefullFunction (); }
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 theMagicalVery UsefullFunction ()'s, including the one from the Base, the firstDerived and at last the secondDerived's one. Is that possible, without a ((secondDerived *)this)->theMagicalVery UsefullFunction () call in the base class (or so?)
You mean like:
void firstDerived::t heMagicalVeryUs efullFunction() {
Base::theMagica lVeryUsefullFun ction(); cout << "Called from the
first
derived class" << endl;
}
void secondDerived:: theMagicalVeryU sefullFunction( ) {
firstDerived::t heMagicalVeryUs efullFunction() ; 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 class version from the base class
pointer and still be able to use a vector with derived element pointers?
class BaseElem { };
class DerivedElem1 { };
|
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 data in my vtable?
RTTI could be implemented this way instead of as a special case for the
compiler. It would also be useful for class specific memory management
implementations.
--
Daniel A. Graifer
|
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 it inline. Like this.
|
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 a pointer
from a base class to a derived class. For example:
class Base{
public:
Base() {}
virtual ~Base() {}
void func1(int);
|
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 pure virtual function body
| |
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;" instead? I think declaring a function as "=0" is the same
|
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 static
function (which I know is illegal), but, is there a way to provide
something similar? The class that is the target of my inquiry is a
template class that interfaces to one of several derived classes
through a pointer to a base class. The...
|
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 Add_Molecule_Req: public Action_Request{
// ......
};
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| | |