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. how to get variable type

how to get variable type

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
24 Posts 9 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.
  • C closecall

    is it possible to check a variable's type? e.g. if m_variable is equal to CString, DoSomething()...

    A Offline
    A Offline
    Andrew Walker
    wrote on last edited by
    #15

    When you get going again, maybe you could try a template specialisation, which avoids the need for typeid or dynamic_cast's by making the compiler do the work for you. This specialisation is so simple that it should :) work even under VC6

    template<class T>
    void f(T t)
    {
        // do nothing
    }
    
    template<>
    void f<CString>(CString t)
    {
        OutputDebugString(t);
    }
    
    // ... snip
        int i = 0;
        f(i);
        CString str = "hello world";
        f(str);
    // ... snip
    

    If you can keep you head when all about you Are losing theirs and blaming it on you; If you can dream - and not make dreams your master; If you can think - and not make thoughts you aim; Yours is the Earth and everything that's in it. Rudyard Kipling

    1 Reply Last reply
    0
    • C Curi0us_George

      Hmm. You're right. Your code's working on VS.NET2003 as well. Perhaps it's an incompatability between CString implementations? Or maybe he's using VC6, and it's failing to match the standards in this area.

      M Offline
      M Offline
      Maxwell Chen
      wrote on last edited by
      #16

      In VC++6, it's an ordinal class CString. In VC++7.x, it's a class template CStringT<typename N>. And typedefed CStringT<char> as CString. Maybe that's the cause... :~


      Maxwell Chen

      C 1 Reply Last reply
      0
      • M Maxwell Chen

        In VC++6, it's an ordinal class CString. In VC++7.x, it's a class template CStringT<typename N>. And typedefed CStringT<char> as CString. Maybe that's the cause... :~


        Maxwell Chen

        C Offline
        C Offline
        Curi0us_George
        wrote on last edited by
        #17

        Apparently so. I guess I'm not quite clear on what counts as a polymorphic type, though. Maybe templates are special or something, but I couldn't find any virtual functions in CString or its superclass. It seems to me that it still shouldn't qualify as a polymorphic type, and therefore dynamic_cast shouldn't work.

        C 1 Reply Last reply
        0
        • C Curi0us_George

          Apparently so. I guess I'm not quite clear on what counts as a polymorphic type, though. Maybe templates are special or something, but I couldn't find any virtual functions in CString or its superclass. It seems to me that it still shouldn't qualify as a polymorphic type, and therefore dynamic_cast shouldn't work.

          C Offline
          C Offline
          closecall
          wrote on last edited by
          #18

          yes i am using vc++6.0 i will try the typeid method once i re-write all my lost codes :| My initial intention was to come up with a global class that can convert a variable of type A to type B at run-time.

          M M 2 Replies Last reply
          0
          • C closecall

            yes i am using vc++6.0 i will try the typeid method once i re-write all my lost codes :| My initial intention was to come up with a global class that can convert a variable of type A to type B at run-time.

            M Offline
            M Offline
            Maxwell Chen
            wrote on last edited by
            #19

            Remember to turn on the RTTI (run-time type information) option in VC++ project setting.


            Maxwell Chen

            C 1 Reply Last reply
            0
            • M Maxwell Chen

              Remember to turn on the RTTI (run-time type information) option in VC++ project setting.


              Maxwell Chen

              C Offline
              C Offline
              closecall
              wrote on last edited by
              #20

              aaaaahh... thats what they mean by run time type information !!! haha.

              1 Reply Last reply
              0
              • C closecall

                yes i am using vc++6.0 i will try the typeid method once i re-write all my lost codes :| My initial intention was to come up with a global class that can convert a variable of type A to type B at run-time.

                M Offline
                M Offline
                Mahendra_786
                wrote on last edited by
                #21

                What vc 7.x is doing seems more logical than vc 6.0 ... it seems to be checking the "from" and "to" types! class A { int a; }; class B: public A { int b; }; /***** break ****/ B b; if (dynamic_cast**(&b)) { printf("OK"); } /***** above will work in vc 7.x but not vc 6.0 */ /***** below will NOT work in both vc 7.x & vc 6.0 */ B b; A *p_a = &b; if (dynamic_cast**(p_a)) { printf("OK"); }****

                1 Reply Last reply
                0
                • C closecall

                  is it possible to check a variable's type? e.g. if m_variable is equal to CString, DoSomething()...

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

                  did you try **typeid**() ??:-D


                  TOXCCT >>> GEII power

                  1 Reply Last reply
                  0
                  • C closecall

                    is it possible to check a variable's type? e.g. if m_variable is equal to CString, DoSomething()...

                    A Offline
                    A Offline
                    Anthony_Yio
                    wrote on last edited by
                    #23

                    For non RTTI way You could try IsKindOf (Of course, this is not as good as RTTI. As this was invented by Microsoft before the time where RTTI was approved by ANSI.) For RTTI you could try type_info or the one they mentiond dynamic_cast.. Sonork 100.41263:Anthony_Yio Life is about experiencing ...

                    1 Reply Last reply
                    0
                    • C closecall

                      is it possible to check a variable's type? e.g. if m_variable is equal to CString, DoSomething()...

                      D Offline
                      D Offline
                      David Crow
                      wrote on last edited by
                      #24

                      For MFC objects, have you looks at IsKindOf()?


                      "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

                      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