Board index » cppbuilder » typeid

typeid


2005-07-06 12:11:20 AM
cppbuilder70
Hi again,
this is possibly a c++ question as much as a borland IDE question but I'll give it a go anyways.
I'm working with a build environment which doesn't really allow me to go making changes to the build options willy nilly. I have lots of objects being passed around that all derive from a base type, Object say (a la Java) but all c++... everything in the system derives from this base type.
At runtime I would like to be able to do something like this;
void some_function(Object* obj)
{
....; // here.....
}
I've inserted a breakpoint at the line marked "here" above and would like to evaluate the following expressions in the de{*word*81};
typeid(obj ).name()
or
typeid( obj ).name()
However whenever I do that the runtime complains that
"E2368 RTTI not available for expression evaluation"
Now can anybody tell me if this is a build option that I need to put in place (that's not really the solution I'm after as it will require a complete rebuild and they'll not like me tampering with build options as it's currently quite sensitive as it stands)
or
is there something else I can put in place in terms of the IDE environment?
If there's an alternative C++ approach to finding the true dynamic type of the "obj" pointer in question, I'd be really grateful for that info too.
thanks much and have a nice day
GrahamO
 
 

Re:typeid

"grahamo" < XXXX@XXXXX.COM >wrote in message
Quote
this is possibly a c++ question as much as a borland IDE question but I'll
give it a go anyways.
This is not an IDE issue. The .language.cpp group would have been more
appropriate.
Quote
void some_function(Object* obj)
You are passing the object by pointer. In order to use typeid with it, you
need to dereference the pointer when calling typeid(), ie:
typeid(*obj).name()
Quote
If there's an alternative C++ approach to finding the true dynamic type of
the "obj" pointer in question, I'd be really grateful for that info too.
Right now, you are just accessing the name() of the class type. But if you
are wanting to actually access the object of the class and do things with
it, you would have to cast the pointer to the appropriate class type. The
C++ language does have dynamic_cast and static_cast operators for that
purpose. For example:
class Object
{
//...
};
class DerivedObject1 : public Object
{
//...
};
class DerivedObject2 : public Object
{
//...
};
void some_function(Object* obj)
{
//...
DerivedObject1 *do1 = dynamic_cast<DerivedObject1*>(obj);
if( do1 )
{
// obj is of type DerivedObject1, do something with
// do1 that is specific to DerivedObject1 ...
}
else
{
DerivedObject2 *do2 = dynamic_cast<DerivedObject2*>(obj);
if( do2 )
{
// obj is of type DerivedObject2, do something with
// do2 that is specific to DerivedObject2 ...
}
//...
}
}
The above function can be short-handed a little bit to the following:
void some_function(Object* obj)
{
//...
if( DerivedObject1 *do1 = dynamic_cast<DerivedObject1*>(obj) )
{
// obj is of type DerivedObject1, do something with
// do1 that is specific to DerivedObject1 ...
}
else if( DerivedObject2 *do2 = dynamic_cast<DerivedObject2*>(obj) )
{
// obj is of type DerivedObject2, do something with
// do2 that is specific to DerivedObject2 ...
}
//...
}
Of course, the best way to use polymorphism is to write code that is not
dependant on any particular class type to begin with. That is what
polymorphism is all about - writing generic type-independant code, by using
virtual methods, ie:
class Object
{
public:
virtual void DoSomething() = 0;
//...
};
class DerivedObject1 : public Object
{
public:
virtual void DoSomething()
{
// perform DerivedObject1-specific operations here...
}
//...
};
class DerivedObject2 : public Object
{
public:
virtual void DoSomething()
{
// perform DerivedObject1-specific operations here...
}
//...
};
void some_function(Object* obj)
{
obj->DoSomething();
}
Gambit
 

Re:typeid

Hi Gambit,
thanks for the response but I think we're talking about different issues here.
Afore I start, I made a typo, I did actually mean to say in my original post;
typeid(obj ).name()
or
typeid( *obj ).name()
BTW both of these work as I tested them in Borland C++ builder before posting. In the case where you don't dereference, the above function call (the first above) returns the name in string form of Obj*, when you dereference it, it gives you the dynamic type of the object. The following code from msdn illustrates the point (I've checked it).
#include <iostream>
#include <typeinfo.h>
class Base {
public:
virtual void vvfunc() {}
};
class Derived : public Base {};
using namespace std;
int main()
{
Derived* pd = new Derived;
Base* pb = pd;
cout << typeid( pb ).name() << endl; //prints "class Base *"
cout << typeid( *pb ).name() << endl; //prints "class Derived"
cout << typeid( pd ).name() << endl; //prints "class Derived *"
cout << typeid( *pd ).name() << endl; //prints "class Derived"
delete pd;
}
In any case, back to the original issue. Is there any way I can get past the error;
"E2368 RTTI not available for expression evaluation"
If there's an option in the IDE available I'd love to put it into action.
thanks much for your response and apologies for the typo in my original mail, it really doesn't help things.
BTW I agree totally with your comments on dynamic_cast etc. you're dead right, unfortunately that's not what I need info on at the moment. Instead, merely for debugging purposes I need to get my hands on the objects typeid and thus string name so I can see what the dynamic type of the object is and thus get a better understand of the code base.
cheers
Graham
You are passing the object by pointer. In order to use typeid with it, you need to dereference the pointer when calling typeid(), ie:
Quote

typeid(*obj).name()

"Remy Lebeau \(TeamB\)" < XXXX@XXXXX.COM >wrote:

"grahamo" < XXXX@XXXXX.COM >wrote in message
news: XXXX@XXXXX.COM ...

>this is possibly a c++ question as much as a borland IDE question but I'll
give it a go anyways.

This is not an IDE issue. The .language.cpp group would have been more
appropriate.

>void some_function(Object* obj)

You are passing the object by pointer. In order to use typeid with it, you need to dereference the pointer when calling typeid(), ie:

typeid(*obj).name()

>If there's an alternative C++ approach to finding the true dynamic type of
>the "obj" pointer in question, I'd be really grateful for that info too.

Right now, you are just accessing the name() of the class type. But if you
are wanting to actually access the object of the class and do things with
it, you would have to cast the pointer to the appropriate class type. The
C++ language does have dynamic_cast and static_cast operators for that
purpose. For example:

class Object
{
//...
};

class DerivedObject1 : public Object
{
//...
};

class DerivedObject2 : public Object
{
//...
};


void some_function(Object* obj)
{
//...
DerivedObject1 *do1 = dynamic_cast<DerivedObject1*>(obj);
if( do1 )
{
// obj is of type DerivedObject1, do something with
// do1 that is specific to DerivedObject1 ...
}
else
{
DerivedObject2 *do2 = dynamic_cast<DerivedObject2*>(obj);
if( do2 )
{
// obj is of type DerivedObject2, do something with
// do2 that is specific to DerivedObject2 ...
}
//...
}
}


The above function can be short-handed a little bit to the following:

void some_function(Object* obj)
{
//...
if( DerivedObject1 *do1 = dynamic_cast<DerivedObject1*>(obj) )
{
// obj is of type DerivedObject1, do something with
// do1 that is specific to DerivedObject1 ...
}
else if( DerivedObject2 *do2 = dynamic_cast<DerivedObject2*>(obj) )
{
// obj is of type DerivedObject2, do something with
// do2 that is specific to DerivedObject2 ...
}
//...
}


Of course, the best way to use polymorphism is to write code that is not
dependant on any particular class type to begin with. That is what
polymorphism is all about - writing generic type-independant code, by using
virtual methods, ie:

class Object
{
public:
virtual void DoSomething() = 0;
//...
};

class DerivedObject1 : public Object
{
public:
virtual void DoSomething()
{
// perform DerivedObject1-specific operations here...
}
//...
};

class DerivedObject2 : public Object
{
public:
virtual void DoSomething()
{
// perform DerivedObject1-specific operations here...
}
//...
};


void some_function(Object* obj)
{
obj->DoSomething();
}


Gambit


 

{smallsort}