how to get variable type
-
is it possible to check a variable's type? e.g. if m_variable is equal to CString, DoSomething()...
-
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 ?
-
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 ?
-
no success CString m_variable = "test"; if ((dynamic_cast(m_variable))!=0)... error C2680: 'class CString' : invalid target type for dynamic_cast
You have to do this way:
CString m_Str = "test"; if(dynamic_cast<CString*****>(**&**m_Str)) DoSomething();
Maxwell Chen
-
is it possible to check a variable's type? e.g. if m_variable is equal to CString, DoSomething()...
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?
-
You have to do this way:
CString m_Str = "test"; if(dynamic_cast<CString*****>(**&**m_Str)) DoSomething();
Maxwell Chen
-
is it possible to check a variable's type? e.g. if m_variable is equal to CString, DoSomething()...
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)
-
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)
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.)
-
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?
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)
-
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?
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
-
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.)
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
-
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
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.
-
is it possible to check a variable's type? e.g. if m_variable is equal to CString, DoSomething()...
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
-
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.
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
-
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
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.
-
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.
-
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.
Remember to turn on the RTTI (run-time type information) option in VC++ project setting.
Maxwell Chen
-
Remember to turn on the RTTI (run-time type information) option in VC++ project setting.
Maxwell Chen