Sign in to follow this  

assignment of reference to consts

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

Sorry, for asking a basic question but I'm confused about why the following to cases aren't analogous:
Foo foo; 
const Foo* const_foo_ptr1 = &foo;
const Foo* const_foo_ptr2 = 0;
const_foo_ptr2 = const_foo_ptr1;  // <- (1) This is fine...
    
const Foo& const_foo_ref1 = foo;
const Foo& const_foo_ref2 = Foo();
const_foo_ref2 =  const_foo_ref1; // <- (2) This isn't...


(2) is giving me an error basically saying that there is no assignment operator with a const Foo& on the left side. The way I understand it
const Foo& ref = bar
means "I can't use ref to mutate bar" and not "ref can't be assigned to anything besides bar". Could someone clear this up?

Share this post


Link to post
Share on other sites
Quote:
Original post by phantom
references can't be reassigned after they are created, regardless of their const status.


Yeah, that rings a bell. However, if i drop the consts on the references the above will compile under VS 2008. What's up with that?
EDIT: oh nevermind I figured out what's going on...

Share this post


Link to post
Share on other sites
Quote:
Original post by jwezorek
Quote:
Original post by phantom
references can't be reassigned after they are created, regardless of their const status.


Yeah, that rings a bell. However, if i drop the consts on the references the above will compile under VS 2008. What's up with that?
EDIT: oh nevermind I figured out what's going on...
And for the benefit of those reading this thread later, what you will have realised is that it's performing copy-assignment on the referenced objects in that case.

Share this post


Link to post
Share on other sites
Quote:
Original post by iMalc
Quote:
Original post by jwezorek
Quote:
Original post by phantom
references can't be reassigned after they are created, regardless of their const status.


Yeah, that rings a bell. However, if i drop the consts on the references the above will compile under VS 2008. What's up with that?
EDIT: oh nevermind I figured out what's going on...
And for the benefit of those reading this thread later, what you will have realised is that it's performing copy-assignment on the referenced objects in that case.


Right, it's just doing the copy. I was having a brain fart there.

Share this post


Link to post
Share on other sites

This topic is 2793 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this