• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
How is this not a complier error? (dynamic casting problem)
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

How is this not a complier error? (dynamic casting problem)


  • Subject: How is this not a complier error? (dynamic casting problem)
  • From: Zachary Pincus <email@hidden>
  • Date: Mon, 25 Jul 2005 18:45:07 -0700

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: This email sent to email@hidden
  • Follow-Ups:
    • Re: How is this not a complier error? (dynamic casting problem)
      • From: Andreas Grosam <email@hidden>
    • Re: How is this not a complier error? (dynamic casting problem)
      • From: Zachary Pincus <email@hidden>
  • Prev by Date: Xcode 1.5 program on Panther not working in Xcode 2.1 in Tiger
  • Next by Date: multi-character character constant and CFString
  • Previous by thread: Xcode 1.5 program on Panther not working in Xcode 2.1 in Tiger
  • Next by thread: Re: How is this not a complier error? (dynamic casting problem)
  • Index(es):
    • Date
    • Thread