typeid operator
-
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.