upcast
-
is there a way I can make this code say "I'm Base!"? class CBase { public: virtual void testApp() { printf("\nI'm base"); } }; class CDerived : public CBase { public: void testApp() { printf("\nI'm derived"); } }; int _tmain(int argc, _TCHAR* argv[]) { CBase* cp = new CDerived(); cp->testApp(); return 0; }
:beer:
-
is there a way I can make this code say "I'm Base!"? class CBase { public: virtual void testApp() { printf("\nI'm base"); } }; class CDerived : public CBase { public: void testApp() { printf("\nI'm derived"); } }; int _tmain(int argc, _TCHAR* argv[]) { CBase* cp = new CDerived(); cp->testApp(); return 0; }
:beer:
Smith# wrote:
is there a way I can make this code say "I'm Base!"?
You mean other than:
CBase* cp = new CBase();
There's also:
void CDerived::testApp()
{
CBase::testApp();
}"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather
-
Smith# wrote:
is there a way I can make this code say "I'm Base!"?
You mean other than:
CBase* cp = new CBase();
There's also:
void CDerived::testApp()
{
CBase::testApp();
}"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather
You should have probably posted this as an answer instead of "general"
-
is there a way I can make this code say "I'm Base!"? class CBase { public: virtual void testApp() { printf("\nI'm base"); } }; class CDerived : public CBase { public: void testApp() { printf("\nI'm derived"); } }; int _tmain(int argc, _TCHAR* argv[]) { CBase* cp = new CDerived(); cp->testApp(); return 0; }
:beer:
David is correct, that's the way to do it, if you don't like his options, then you probably shouldn't have made it virtual, if its not virtual, then the call gets evaluated at runtime according to the pointer. if not virtual...
CBase* cp = new CDerived();
cp->testApp(); //calls base testApp() because pointer is of type CBase
((CDerived *)cp)->testApp(); //calls derived testApp() because pointer is of type CDerived..you just have to know exactly what it is you want.
-
is there a way I can make this code say "I'm Base!"? class CBase { public: virtual void testApp() { printf("\nI'm base"); } }; class CDerived : public CBase { public: void testApp() { printf("\nI'm derived"); } }; int _tmain(int argc, _TCHAR* argv[]) { CBase* cp = new CDerived(); cp->testApp(); return 0; }
:beer:
This should do it. Although I'm not at all sure why you'd want that.
int _tmain(int argc, _TCHAR* argv[])
{
CBase* cp = new CDerived();
cp->CBase::testApp();
return 0;
} -
David is correct, that's the way to do it, if you don't like his options, then you probably shouldn't have made it virtual, if its not virtual, then the call gets evaluated at runtime according to the pointer. if not virtual...
CBase* cp = new CDerived();
cp->testApp(); //calls base testApp() because pointer is of type CBase
((CDerived *)cp)->testApp(); //calls derived testApp() because pointer is of type CDerived..you just have to know exactly what it is you want.
-
So once we have said virtual, it's dead No? to access the base? Not even a hack available to do that? may be through manipulating vptrs?
:beer:
Its not dead, its just the whole purpose behind the "virtual" keyword... that the base method can be overriden... stefan's solution probably works as well, but again, you're overriding a behavior that is designed.
-
So once we have said virtual, it's dead No? to access the base? Not even a hack available to do that? may be through manipulating vptrs?
:beer:
...and by the way, the derived class doesn't HAVE to override a virtual method in the base, its optional, you only HAVE to override pure virtuals.
-
This should do it. Although I'm not at all sure why you'd want that.
int _tmain(int argc, _TCHAR* argv[])
{
CBase* cp = new CDerived();
cp->CBase::testApp();
return 0;
} -
So once we have said virtual, it's dead No? to access the base? Not even a hack available to do that? may be through manipulating vptrs?
:beer:
See my answer below: you can still call methods from the base class(es) even if the pointer you use to call it points to a class that has overridden that implementation:
CBase* pbase = new CBase;
CDerived* pDerived = new CDerived;
pbase->testApp(); // "I'm Base"
pDerived->testApp(); // I'm Derived"
pDerived->CBase::testApp(); // "I'm Base"You can invoke methods of base classes by adding the name of that class, separated by '::' to the name of the function you want to call. In doing so you ignore the virtual function table for that call only. You can freely mix calls of that type with normal calls that use the virtual functions, as you can see in the example above.
-
this isn't what you'd call normal use...