Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. typeid operator

typeid operator

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
16 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K kfaday

    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?

    J Offline
    J Offline
    jmkhael
    wrote on last edited by
    #4

    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 ) ;

    K 1 Reply Last reply
    0
    • J jmkhael

      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 ) ;

      K Offline
      K Offline
      kfaday
      wrote on last edited by
      #5

      i tried this but it doesn't work cvehicle *vtemp=(list->Getvehicle()); if (typeid(vtemp)==typeid(ccar))) return vtemp;

      J 1 Reply Last reply
      0
      • K kfaday

        i tried this but it doesn't work cvehicle *vtemp=(list->Getvehicle()); if (typeid(vtemp)==typeid(ccar))) return vtemp;

        J Offline
        J Offline
        jmkhael
        wrote on last edited by
        #6

        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 ) ;

        K 1 Reply Last reply
        0
        • J jmkhael

          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 ) ;

          K Offline
          K Offline
          kfaday
          wrote on last edited by
          #7

          t and t1 are always diferent! from the debugger: t: + _m_d_name 0x00433890 ".PAVcvehicle@@" t1: + _m_d_name 0x00433850 ".?AVccar@@"

          J 1 Reply Last reply
          0
          • K kfaday

            t and t1 are always diferent! from the debugger: t: + _m_d_name 0x00433890 ".PAVcvehicle@@" t1: + _m_d_name 0x00433850 ".?AVccar@@"

            J Offline
            J Offline
            jmkhael
            wrote on last edited by
            #8

            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 ) ;

            K 1 Reply Last reply
            0
            • J jmkhael

              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 ) ;

              K Offline
              K Offline
              kfaday
              wrote on last edited by
              #9

              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

              J T 2 Replies Last reply
              0
              • K kfaday

                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

                J Offline
                J Offline
                jmkhael
                wrote on last edited by
                #10

                cvehicle *vtemp=(list->Getvehicle()); if (typeid(*vtemp)==typeid(ccar))) return vtemp; Papa while (TRUE) Papa.WillLove ( Bebe ) ;

                K 1 Reply Last reply
                0
                • J jmkhael

                  cvehicle *vtemp=(list->Getvehicle()); if (typeid(*vtemp)==typeid(ccar))) return vtemp; Papa while (TRUE) Papa.WillLove ( Bebe ) ;

                  K Offline
                  K Offline
                  kfaday
                  wrote on last edited by
                  #11

                  it gives me an error: (runtime error, it builds alright) unhandled exception

                  J 1 Reply Last reply
                  0
                  • K kfaday

                    it gives me an error: (runtime error, it builds alright) unhandled exception

                    J Offline
                    J Offline
                    jmkhael
                    wrote on last edited by
                    #12

                    "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 ) ;

                    K 1 Reply Last reply
                    0
                    • J jmkhael

                      "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 ) ;

                      K Offline
                      K Offline
                      kfaday
                      wrote on last edited by
                      #13

                      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

                      A 1 Reply Last reply
                      0
                      • K kfaday

                        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

                        T Offline
                        T Offline
                        toxcct
                        wrote on last edited by
                        #14

                        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

                        1 Reply Last reply
                        0
                        • K kfaday

                          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

                          A Offline
                          A Offline
                          antlers
                          wrote on last edited by
                          #15

                          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.

                          K 1 Reply Last reply
                          0
                          • A antlers

                            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.

                            K Offline
                            K Offline
                            kfaday
                            wrote on last edited by
                            #16

                            that did it!!!!!!!!!!!!!1 thanks!!

                            1 Reply Last reply
                            0
                            Reply
                            • Reply as topic
                            Log in to reply
                            • Oldest to Newest
                            • Newest to Oldest
                            • Most Votes


                            • Login

                            • Don't have an account? Register

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • World
                            • Users
                            • Groups