CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 16

Threaded View

  1. #1
    Join Date
    Aug 2002
    Posts
    78

    static_cast vs. dynamic_cast and reinterpret_cast

    Let's suppose I have an array of a base class, but it's filled with derived classes. I'll use only one derived class, but suppose there are several types.

    Code:
    Edit: wrong! CBase myArray[MY_ARRAYSIZE];
    
    What I meant:  CBase *myArray[MY_ARRAYSIZE];
    
    ...
    
    reinterpret_cast<CDerived *>(myArray[i])->derivedFunction();

    According to the descriptions of the various castings, this is the wrong thing to do since it doesn't "climb the trees" to return the correct pointer (where a pointer to the derived might actually != pointer to the same object's base!)

    So I'd want to use dynamic_cast or static_cast instead (static_cast would be good enough for this particular application.)


    More importantly, reinterpret_cast is wrong to use in this situation always?
    Last edited by Gorgor; April 16th, 2004 at 09:03 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured