Re: How is this not a complier error? (dynamic casting problem)
Re: How is this not a complier error? (dynamic casting problem)
- Subject: Re: How is this not a complier error? (dynamic casting problem)
- From: Zachary Pincus <email@hidden>
- Date: Mon, 25 Jul 2005 19:41:33 -0700
Sorry, forgot to mention the vital stats:
XCode 2.1, gcc 4.0, OS X 10.4.2
Zach
On Jul 25, 2005, at 6:45 PM, Zachary Pincus wrote:
Hello,
I'm running into a problem in my C++ code (which uses RTTI and
dynamic_cast) which I can only account to be a g++ compiler error.
However, I've recognized that most of the time people think they
have compiler errors, they're really just being obtuse. So perhaps
someone can show me where my obtuseness lies, and how I might fix it!
Here's the basic set-up. I've got a simple class hierarchy:
template<ImageDimension> itk::ImageBase;
template<class PixelType, ImageDimension> itk::Image
: public itk::ImageBase<ImageDimension>;
That is, Image is a subclass of ImageBase, for a given image
dimensionality.
Now that we've got that over with, here's the offending code. This
just takes an Image* pointer and up-casts it to ImageBase*, and
then tries to down-cast it (all with dynamic_cast).
const itk::Image<float, 2>* imageP = input.GetPointer();
// grab an image from somewhere.
const itk::ImageBase<2>* imageBaseP = dynamic_cast<const
itk::ImageBase<2>*>(imageP);
// above cast succeeds
const itk::Image<float, 2>* imagePDowncast = dynamic_cast<const
itk::Image<float, 2>*>(imageBaseP);
// what? This cast fails!
At the end of the email is some code that prints the runtime types
and confirms the failure better, in case you want to see it.
Now, this looks like a really simple exercise. What's the problem
here? I just can't see it! (Again, see below for more details.)
Zach Pincus
Program in Biomedical Informatics and Department of Biochemistry
Stanford University School of Medicine
OK, here's the "test" code I used to verify the casting problems:
----------
std::cout << "imageP type : " << typeid(imageP).name() <<
std::endl;
std::cout << "imageBaseP type : " << typeid(imageBaseP).name() <<
std::endl;
std::cout << "*imageP type : "<< typeid(*imageP).name() <<
std::endl;
if (imageBaseP) {
std::cout << "*imageBaseP type: "<< typeid(*imageBaseP).name() <<
std::endl;
std::cout << "Up-cast successful. Checking down-cast." << std::endl;
if (imagePDowncast) {
std::cout << "*imagePDowncast type: "<< typeid
(*imagePDowncast).name() << std::endl;
} else {
std::cout << "Down-cast back to derived class NOT SUCCESSFUL!"
<< std::endl;
}
} else {
std::cout << "imageBaseP is NULL" << std::endl;
}
-----------
and here's the output (run through the demangler to get real C++
names):
imageP type : itk::Image<float, 2u> const*
imageBaseP type : itk::ImageBase<2u> const*
*imageP type : itk::Image<float, 2u>
*imageBaseP type: itk::Image<float, 2u>
Up-cast successful. Checking down-cast.
Down-cast back to derived Checking NOT SUCCESSFUL!
So, one oddity I notice is that the run-time type of the
dereferenced pointers is NOT const, even though it should be. But I
see no reason that imageBaseP, which points to an itk::Image
object, couldn't be down-cast to the same! Any thoughts?
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
40stanford.edu
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden