typeid operator
-
do you know MSDN ? i cite :
typeid Operator
C++ Specific —>typeid( type-id ) typeid( expression )
The
typeid
operator allows the type of an object to be determined at run-time.The result of a
typeid
expression is aconst type_info&
. The value is a reference to atype_info
object that represents either the type-id or
the type of the expression, depending on which form oftypeid
is used.
See type_info Class for more information.END C++ Specific
here you are
TOXCCT >>> GEII power
-
sorry, i'm new to c++ and i don't have msdn cds. would you mind giving me an example of typeid usage, if it's not too much trouble?
string clss; if (typeid(obj)==typeid(int)) clss = "I"; else if (typeid(obj)==typeid(unsigned int)) clss = "i"; else if (typeid(obj)==typeid(float)) clss = "F"; else if (typeid(obj)==typeid(double)) clss = "D"; else if (typeid(obj)==typeid(short)) clss = "B"; else if (typeid(obj)==typeid(unsigned short)) clss = "b"; else if (typeid(obj)==typeid(long)) clss = "I"; else if (typeid(obj)==typeid(unsigned long)) clss = "i"; got the idea? :) Papa while (TRUE) Papa.WillLove ( Bebe ) ;
-
string clss; if (typeid(obj)==typeid(int)) clss = "I"; else if (typeid(obj)==typeid(unsigned int)) clss = "i"; else if (typeid(obj)==typeid(float)) clss = "F"; else if (typeid(obj)==typeid(double)) clss = "D"; else if (typeid(obj)==typeid(short)) clss = "B"; else if (typeid(obj)==typeid(unsigned short)) clss = "b"; else if (typeid(obj)==typeid(long)) clss = "I"; else if (typeid(obj)==typeid(unsigned long)) clss = "i"; got the idea? :) Papa while (TRUE) Papa.WillLove ( Bebe ) ;
-
i tried this but it doesn't work cvehicle *vtemp=(list->Getvehicle()); if (typeid(vtemp)==typeid(ccar))) return vtemp;
just tried this and it works CString temp; CString temp1; const type_info& t = typeid(temp); AfxMessageBox (t.name ()); // It gave class CString if(typeid(temp1) == typeid(CString)) AfxMessageBox ("Matched"); // It said matched Check this too: class Base { ... }; class Derived : public Base { ... }; void f() { Derived* pd = new Derived; Base* pb = pd; ... const type_info& t = typeid(pb); // t holds pointer type_info const type_info& t1 = typeid(*pb); // t1 holds Derived info ... } (This is your prob, you should dereference the pointer) What does yours do? Papa while (TRUE) Papa.WillLove ( Bebe ) ;
-
just tried this and it works CString temp; CString temp1; const type_info& t = typeid(temp); AfxMessageBox (t.name ()); // It gave class CString if(typeid(temp1) == typeid(CString)) AfxMessageBox ("Matched"); // It said matched Check this too: class Base { ... }; class Derived : public Base { ... }; void f() { Derived* pd = new Derived; Base* pb = pd; ... const type_info& t = typeid(pb); // t holds pointer type_info const type_info& t1 = typeid(*pb); // t1 holds Derived info ... } (This is your prob, you should dereference the pointer) What does yours do? Papa while (TRUE) Papa.WillLove ( Bebe ) ;
-
t and t1 are always diferent! from the debugger: t: + _m_d_name 0x00433890 ".PAVcvehicle@@" t1: + _m_d_name 0x00433850 ".?AVccar@@"
-
yes so check it against t1 dereference first the pointer you receive and then compare it to typeid(ccar) Papa while (TRUE) Papa.WillLove ( Bebe ) ;
-
Papa wrote: dereference first the pointer you receive and then compare it to typeid(ccar) what do you mean by this? sorry, english isn't my first language
-
cvehicle *vtemp=(list->Getvehicle()); if (typeid(*vtemp)==typeid(ccar))) return vtemp; Papa while (TRUE) Papa.WillLove ( Bebe ) ;
-
"If the expression is dereferencing a pointer, and that pointer’s value is zero, typeid throws a bad_typeid exception. If the pointer does not point to a valid object, a __non_rtti_object exception is thrown." Do you have a NULL pointer? Papa while (TRUE) Papa.WillLove ( Bebe ) ;
-
"If the expression is dereferencing a pointer, and that pointer’s value is zero, typeid throws a bad_typeid exception. If the pointer does not point to a valid object, a __non_rtti_object exception is thrown." Do you have a NULL pointer? Papa while (TRUE) Papa.WillLove ( Bebe ) ;
-
Papa wrote: dereference first the pointer you receive and then compare it to typeid(ccar) what do you mean by this? sorry, english isn't my first language
you deference a pointer when you don't get the address it contains but when you get what is at the pointed address.
char* pc = (char*)malloc(sizeof(char)); // my pointer (something like 0x0A78DQ43)
*pc = 'a'; // dereferenced pointer (acsii code for 'a' caracter)pc : simple pointer (cointaining an address)
*pc : dereferenced pointer (value at the pointed address)
TOXCCT >>> GEII power
-
no, *vtemp isn't null it gives me this warning warning C4541: 'typeid' used on polymorphic type 'class ccar' with /GR-; unpredictable behavior may result
-
typeid depends on RTTI being turned on. RTTI is part of the language standard but C++ turns it off by default. Change your project settings to enable RTTI and you will get rid of the compile-time warning and the run-time exception.