Generic Usenet Account
2007-12-12 20:40:09 UTC
I ran a small experiment involving RTTI and dynamic casting.
Basically what I did was to cast a base class pointer to a derived
type (yes, I know that is not kosher). I then performed
dynamic_casting and I invoked RTTI. In both instances, the run time
environment did not pick up the fact that what I was claiming to be a
derived pointer was in fact the base pointer. However, when I invoked
a virtual method using the ostensibly derived class pointer (which was
in reality the base class pointer), it was the base class method that
got invoked (and correctly so). Is this the correct behavior, or is
it the quirk of my compiler (gcc version 3.3.1)?
I know that I am a nobody, but here is what I was expecting:
dynamic_casting: Since the pointer was really to the base class, and
not to the derived class, as claimed, dynamic_casting should have
picked that up and returned NULL.
RTTI: The typeid for the pointer should have been that of the base,
even though I claimed that it was the derived class pointer.
My sample code snippet follows:
//////////////////////////////////////////////////////////
#include <iostream>
#include <typeinfo>
#include <string>
using namespace std;
#define TYPEID(x) cout << #x << ": " << typeid(x).name() << endl;
class Base
{
public:
Base() {}
~Base() {}
virtual void testMethod (int x) {cout << "Passed value " << x << "
to the base class method\n";}
};
class Derived: public Base
{
public:
Derived() {}
~Derived() {}
virtual void testMethod (int x) {cout << "Passed value " << x << "
to the derived class method\n";}
};
template<typename T>
void
validateType(const string& label, T* ptr)
{
T* typePtr = dynamic_cast<T*>(ptr);
if(ptr)
cout << "Type match";
else
cout << "Type mismatch";
cout << " for " << label << endl;
}
main()
{
Base *basePtr1 = new Base;
Base *basePtr2 = new Base;
Derived *derivedPtr = static_cast<Derived *>(basePtr2);
basePtr1->testMethod(25);
derivedPtr->testMethod(25);
validateType<Base>("basePtr", basePtr1);
validateType<Derived>("Derived", derivedPtr);
TYPEID(basePtr1);
TYPEID(basePtr2);
TYPEID(derivedPtr);
return 0;
}
//////////////////////////////////////////////////////////
Thanks,
Song
Basically what I did was to cast a base class pointer to a derived
type (yes, I know that is not kosher). I then performed
dynamic_casting and I invoked RTTI. In both instances, the run time
environment did not pick up the fact that what I was claiming to be a
derived pointer was in fact the base pointer. However, when I invoked
a virtual method using the ostensibly derived class pointer (which was
in reality the base class pointer), it was the base class method that
got invoked (and correctly so). Is this the correct behavior, or is
it the quirk of my compiler (gcc version 3.3.1)?
I know that I am a nobody, but here is what I was expecting:
dynamic_casting: Since the pointer was really to the base class, and
not to the derived class, as claimed, dynamic_casting should have
picked that up and returned NULL.
RTTI: The typeid for the pointer should have been that of the base,
even though I claimed that it was the derived class pointer.
My sample code snippet follows:
//////////////////////////////////////////////////////////
#include <iostream>
#include <typeinfo>
#include <string>
using namespace std;
#define TYPEID(x) cout << #x << ": " << typeid(x).name() << endl;
class Base
{
public:
Base() {}
~Base() {}
virtual void testMethod (int x) {cout << "Passed value " << x << "
to the base class method\n";}
};
class Derived: public Base
{
public:
Derived() {}
~Derived() {}
virtual void testMethod (int x) {cout << "Passed value " << x << "
to the derived class method\n";}
};
template<typename T>
void
validateType(const string& label, T* ptr)
{
T* typePtr = dynamic_cast<T*>(ptr);
if(ptr)
cout << "Type match";
else
cout << "Type mismatch";
cout << " for " << label << endl;
}
main()
{
Base *basePtr1 = new Base;
Base *basePtr2 = new Base;
Derived *derivedPtr = static_cast<Derived *>(basePtr2);
basePtr1->testMethod(25);
derivedPtr->testMethod(25);
validateType<Base>("basePtr", basePtr1);
validateType<Derived>("Derived", derivedPtr);
TYPEID(basePtr1);
TYPEID(basePtr2);
TYPEID(derivedPtr);
return 0;
}
//////////////////////////////////////////////////////////
Thanks,
Song