Jump to content
  • Advertisement
Sign in to follow this  
chaosgame

type operator=( const type& )

This topic is 4826 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

I have a class sample with a method get_x() to return the value of a private pointer x. I need to access x from the function operator=() but i get an error saying that I cant access the value of x through the function because it has to be constant. should i use a cast?

Share this post


Link to post
Share on other sites
Advertisement
Chances are that you just need a const version of your get_x() member function.

edit: On second thought. Why can't you just access x directly? Is x a member of a base class?

Share this post


Link to post
Share on other sites
The problem is that type::get_x() is not available in const objects because of the way it is declared. In order for your function to be available to const objects, it must be declared like this:
    P const * get_x() const; 
The const says that get_x() will not modify the object. The const in "P const *" is not required, but if get_x() won't modify the object, the pointer should be restricted too. You will frequently see code like this:
    P const * get_x() const;
P * get_x();
The second function allows get_x() to return a pointer to non-const (but only for a non-const object).

Share this post


Link to post
Share on other sites
the function is sample::sample( const sample & origional ) and i need to access origional.get_x(). how do i make it return a const? const void * sample::get_x(){ return x; }?

Share this post


Link to post
Share on other sites
const void * sample::get_x() const { return x; }

Without the trailing const, you can't access get_x() through a const sample.

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!