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 Offline
    C Offline
    closecall
    wrote on last edited by
    #1

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

    K C C A T 7 Replies Last reply
    0
    • C closecall

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

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

      if ((dynamic_cast(m_variable))!=0) { DoSomething(); } hope it helps

      C 1 Reply Last reply
      0
      • K kfaday

        if ((dynamic_cast(m_variable))!=0) { DoSomething(); } hope it helps

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

        unfortunately that didn't work. error C2682: cannot use dynamic_cast to convert from 'class CString' to 'class CString *' and const_cast didn't work either (not that I fully understand either of them :-D) changing CString to double didn't help - apparently it's not possible to dynamic_cast double* anymore ideas ?

        K 1 Reply Last reply
        0
        • C closecall

          unfortunately that didn't work. error C2682: cannot use dynamic_cast to convert from 'class CString' to 'class CString *' and const_cast didn't work either (not that I fully understand either of them :-D) changing CString to double didn't help - apparently it's not possible to dynamic_cast double* anymore ideas ?

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

          what if you remove that * with dynamic_cast?

          C 1 Reply Last reply
          0
          • K kfaday

            what if you remove that * with dynamic_cast?

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

            no success CString m_variable = "test"; if ((dynamic_cast(m_variable))!=0)... error C2680: 'class CString' : invalid target type for dynamic_cast

            M 1 Reply Last reply
            0
            • C closecall

              no success CString m_variable = "test"; if ((dynamic_cast(m_variable))!=0)... error C2680: 'class CString' : invalid target type for dynamic_cast

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

              You have to do this way: CString m_Str = "test"; if(dynamic_cast<CString*****>(**&**m_Str))   DoSomething();


              Maxwell Chen

              C 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()...

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

                If you already have the variable, as opposed to a pointer or a reference to a variable, you already know the type of the variable, so I'm not quite clear on why you even need to check this at all. If you can post a code snippet showing what exactly you are trying to do, I'm sure someone could give you an exact answer. At least tell us what m_variable is declared to be? Is it a CString, or a pointer to a CString, or a void pointer, or something else?

                1 Reply Last reply
                0
                • M Maxwell Chen

                  You have to do this way: CString m_Str = "test"; if(dynamic_cast<CString*****>(**&**m_Str))   DoSomething();


                  Maxwell Chen

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

                  that didn't work either. i tried: CString m_Str = "test"; if(dynamic_cast(&m_Str)) TRACE("yes !\n") else TRACE("no !\n"); error C2683: dynamic_cast : 'CString' is not a polymorphic type do i have to declare it as a polymorphic type?

                  C M 2 Replies Last reply
                  0
                  • C closecall

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

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

                    my source file is gone ! (not in Recycle Bin, My Computer, anywhere...) for some reason, my source file has disappeared (although the header file is still there). Could it be because I wrote #include "MyGlobals.h" in stdafx.h ? I've been using extern to make everything in MyGlobals.cpp available everywhere - and it worked so well for so long... up until I started messing around with dynamic_cast. X| has this happened to anyone before ? :doh:(in extreme doh)

                    C 1 Reply Last reply
                    0
                    • C closecall

                      my source file is gone ! (not in Recycle Bin, My Computer, anywhere...) for some reason, my source file has disappeared (although the header file is still there). Could it be because I wrote #include "MyGlobals.h" in stdafx.h ? I've been using extern to make everything in MyGlobals.cpp available everywhere - and it worked so well for so long... up until I started messing around with dynamic_cast. X| has this happened to anyone before ? :doh:(in extreme doh)

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

                      I'd say run a system-wide search for the file just to make sure you didn't accidentally drag-and-drop it into another folder somehow. If that doesn't turn it up, it's probably gone for good. I suppose you could try some file-recovery software. But I've had little success with that in the past. I'd also run a system-wide virus scan. If it's really gone, something deleted it. It's unlikely that it's a Windows or hardware problem. (Might be worth checking the event viewer in Administrative Tools, though, just in case.) After that, I guess you get to start over if you didn't recover it. Sucks, but it happens. (This is why everyone talks about backups, though I don't believe most actually do so on a regular basis.)

                      M 1 Reply Last reply
                      0
                      • C closecall

                        that didn't work either. i tried: CString m_Str = "test"; if(dynamic_cast(&m_Str)) TRACE("yes !\n") else TRACE("no !\n"); error C2683: dynamic_cast : 'CString' is not a polymorphic type do i have to declare it as a polymorphic type?

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

                        I typed up a reply explaining that I'm pretty certain you need to qualify the dynamic_cast in this case (i.e. add <CString *>), but that won't fix your problem here. CString has no virtual functions (apparently) and as such, is not polymorphic, which means you cannot use dynamic_cast on it. Use typeid instead. It should theoretically be faster anyway. typeid Operator (MSDN)

                        1 Reply Last reply
                        0
                        • C closecall

                          that didn't work either. i tried: CString m_Str = "test"; if(dynamic_cast(&m_Str)) TRACE("yes !\n") else TRACE("no !\n"); error C2683: dynamic_cast : 'CString' is not a polymorphic type do i have to declare it as a polymorphic type?

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

                          Don't know... Regarding to typename CString specifically, it works well with my VC++.NET v7.0. I had tested the code snippet before I posted the previous post. And the code compiles well without any error / warning!


                          Maxwell Chen

                          C 1 Reply Last reply
                          0
                          • C Curi0us_George

                            I'd say run a system-wide search for the file just to make sure you didn't accidentally drag-and-drop it into another folder somehow. If that doesn't turn it up, it's probably gone for good. I suppose you could try some file-recovery software. But I've had little success with that in the past. I'd also run a system-wide virus scan. If it's really gone, something deleted it. It's unlikely that it's a Windows or hardware problem. (Might be worth checking the event viewer in Administrative Tools, though, just in case.) After that, I guess you get to start over if you didn't recover it. Sucks, but it happens. (This is why everyone talks about backups, though I don't believe most actually do so on a regular basis.)

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

                            I used to zip my source code files (removed those .ncb, .opt, .plg, .\Debug, etc.) with separate file names (timestamp + hint) quite often to prevent such disasaters.


                            Maxwell Chen

                            1 Reply Last reply
                            0
                            • M Maxwell Chen

                              Don't know... Regarding to typename CString specifically, it works well with my VC++.NET v7.0. I had tested the code snippet before I posted the previous post. And the code compiles well without any error / warning!


                              Maxwell Chen

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

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