Jump to content
  • Advertisement
Sign in to follow this  
adder_noir

New Style Cast Operator Question

This topic is 2858 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,

Hit the latest odd thing in my book. I'm fairly sure what it means but as always I would like your help. It's about the new cast operators. Here's an example of an easier one I pretty much understand:

Shape* sp = new Circle;
Circle* cp = static_cast<Circle*>(sp);



This creates a pointer to Shape, which actually points to a Circle, which as I understand is ok because I've defined Circle as a derived class from the Shape class.

The cast operation creates a new pointer to Circle, and sets its value to the value of 'sp' cast to a Circle*. But surprising really that you still have to cast 'sp' to a Circle* even though it's already initialised as pointing to a circle object but they that's C++ ;o)

This next bit I'm unsure of:

const Shape* getNextShape{....} // define function returning a const Shape*
//....

Circle* cp = static_cast<Circle*>(const_cast<Shape*>(getNextShape()))



Ok. From what I can get here it *looks* like the:

const_cast<Shape*>(getNextShape())

part is getting the returned pointer from getNextShape() and casting it to a non-const value, i.e. a Shape* not a const Shape*. I'm unsure if this means a constant pointer or a pointer to a constant Shape, either way looks like const_cast has removed the const part. Am I correct? THe rest of the code:

Circle* cp = static_cast<Circle*>....

appears to be casting the returned Shape* pointer to a Circle* pointer and assigning this value to the variable 'cp'.

I'd appreciate any comments or help anyone could offer on this and confirm if I am correct. I've probably got more of my own observations to add too that will no doubt be prompted by input from others. Thanks.

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by adder_noir
The cast operation creates a new pointer to Circle, and sets its value to the value of 'sp' cast to a Circle*. But surprising really that you still have to cast 'sp' to a Circle* even though it's already initialised as pointing to a circle object but they that's C++ ;o)


When you said that sp is a Shape*, and assigned the new Circle to sp, you basically commanded the compiler to forget that the pointed-at thing is actually a Circle. The variable sp points at Shapes, and that's all we know about it.

The static_cast here is saying "I know you (by design) have the memory of a goldfish; but trust me on this one - the pointed-at thing really is a Circle this time".

Quote:
is getting the returned pointer from getNextShape() and casting it to a non-const value, i.e. a Shape* not a const Shape*.


Yes.

You should basically never do this, of course. The function gave you const-whatever for a reason.

Quote:
I'm unsure if this means a constant pointer or a pointer to a constant Shape


Read the declarations backwards (right to left).

Quote:
appears to be casting the returned Shape* pointer to a Circle* pointer and assigning this value to the variable 'cp'.


Yes; same as before.

Share this post


Link to post
Share on other sites
Thanks Karl!

I pretty much understand all that thanks for taking the time to write it all out. I'm currently working through that link you posted and I can feel a new topic coming on ;o)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!