Dynamic cast problem
-
I migrated my project to VC2005 and found a pretty strange problem. The code tries to dynamic cast an object and the result is NULL even the objet type is valid. E.g. I have a following code:
class A; A* ptr = new A; ... A* pTest = dynamic_cast(ptr);
pTest is NULL but in the debugger I can see that "ptr" is A. The project has RTTI enabled. Any idea what could be the problem? Thanks, Abyss -
I migrated my project to VC2005 and found a pretty strange problem. The code tries to dynamic cast an object and the result is NULL even the objet type is valid. E.g. I have a following code:
class A; A* ptr = new A; ... A* pTest = dynamic_cast(ptr);
pTest is NULL but in the debugger I can see that "ptr" is A. The project has RTTI enabled. Any idea what could be the problem? Thanks, AbyssTry this:
pTest = ptr;
- it might work just fine for you. Good luck! -
I migrated my project to VC2005 and found a pretty strange problem. The code tries to dynamic cast an object and the result is NULL even the objet type is valid. E.g. I have a following code:
class A; A* ptr = new A; ... A* pTest = dynamic_cast(ptr);
pTest is NULL but in the debugger I can see that "ptr" is A. The project has RTTI enabled. Any idea what could be the problem? Thanks, Abyss -
try this A* pTest = dynamic_cast(ptr); but in ur case it is not required as suggested by Mihai Moga never say die -- modified at 7:37 Wednesday 12th April, 2006
-
I migrated my project to VC2005 and found a pretty strange problem. The code tries to dynamic cast an object and the result is NULL even the objet type is valid. E.g. I have a following code:
class A; A* ptr = new A; ... A* pTest = dynamic_cast(ptr);
pTest is NULL but in the debugger I can see that "ptr" is A. The project has RTTI enabled. Any idea what could be the problem? Thanks, AbyssAbyss wrote:
A* pTest = dynamic_cast(ptr);
What language is this ? In C++, it would be :
A *pTest = dynamic_cast<A*>(ptr);
You are not giving the target pointer, thus getting a void*, e.g.
NULL
. Anyway, in this example, as others said, this works without problem:A *pTest=ptr;
~RaGE(); -- modified at 7:31 Wednesday 12th April, 2006
-
sunit5 wrote:
A* pTest = dynamic_cast(ptr);
:wtf: what is your suggestion ? exactly what he wrote...
-
I migrated my project to VC2005 and found a pretty strange problem. The code tries to dynamic cast an object and the result is NULL even the objet type is valid. E.g. I have a following code:
class A; A* ptr = new A; ... A* pTest = dynamic_cast(ptr);
pTest is NULL but in the debugger I can see that "ptr" is A. The project has RTTI enabled. Any idea what could be the problem? Thanks, AbyssThe "<" and ">” characters and what's in between is not showing up on your post. Steve
-
This is my suggestion A* pTest = dynamic_cast<A*>(ptr); i didnot put <> (as in formatting )so <A*> was not visible never say die -- modified at 7:40 Wednesday 12th April, 2006
Finally, I found the problem. Well I simplified my problem so much. In my case the problem is somewhere else:
A* ptr = new A;
Then I incorrectly static casted the pointer to a C object:C* pObj = static_cast(ptr);
because of this the dynamic cast failed in VC8, however in VC7 it works fine. pTest will be NULL in VC8 and in VC7 it will be ptr.A* pTest = dynamic_cast(pObj);
I know the given approach is incorrect - why to cast to C object?! Just want to point out that VC8 is less tolerant... and this is good. On the other side in case of big projects the code migration to VC8 can cause big pains :mad: Thanks, Abyss