# assignment of reference to consts

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

## 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 on other sites
references can't be reassigned after they are created, regardless of their const status.

##### Share on other sites
Quote:
 Original post by phantomreferences 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 on other sites
Quote:
Original post by jwezorek
Quote:
 Original post by phantomreferences 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 on other sites
Quote:
Original post by iMalc
Quote:
Original post by jwezorek
Quote:
 Original post by phantomreferences 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.

1. 1
Rutin
25
2. 2
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 14
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631757
• Total Posts
3002141
×