|
-
April 15th, 2004, 04:33 PM
#1
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|