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. Dynamic cast problem

Dynamic cast problem

Scheduled Pinned Locked Moved C / C++ / MFC
debugginghelpquestion
8 Posts 6 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.
  • A Offline
    A Offline
    Abyss
    wrote on last edited by
    #1

    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

    stefanmihaimogaS S R S 4 Replies Last reply
    0
    • A 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, Abyss

      stefanmihaimogaS Offline
      stefanmihaimogaS Offline
      stefanmihaimoga
      wrote on last edited by
      #2

      Try this: pTest = ptr; - it might work just fine for you. Good luck!

      1 Reply Last reply
      0
      • A 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, Abyss

        S Offline
        S Offline
        sunit5
        wrote on last edited by
        #3

        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

        T 1 Reply Last reply
        0
        • S sunit5

          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

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

          sunit5 wrote:

          A* pTest = dynamic_cast(ptr);

          :wtf: what is your suggestion ? exactly what he wrote...

          S 1 Reply Last reply
          0
          • A 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, Abyss

            R Offline
            R Offline
            Rage
            wrote on last edited by
            #5

            Abyss 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

            1 Reply Last reply
            0
            • T toxcct

              sunit5 wrote:

              A* pTest = dynamic_cast(ptr);

              :wtf: what is your suggestion ? exactly what he wrote...

              S Offline
              S Offline
              sunit5
              wrote on last edited by
              #6

              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

              A 1 Reply Last reply
              0
              • A 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, Abyss

                S Offline
                S Offline
                Stephen Hewitt
                wrote on last edited by
                #7

                The "<" and ">” characters and what's in between is not showing up on your post. Steve

                1 Reply Last reply
                0
                • S sunit5

                  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

                  A Offline
                  A Offline
                  Abyss
                  wrote on last edited by
                  #8

                  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

                  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