Recently I have been learning about good programming practice in C++ and found out that many programs pass objects to functions by reference so that multiple instances are not created. I have also learned that passing a constant reference prevents the original object from being modified however I do not understand how this works exactly. Shouldn't a constant reference create a new instance because the original object cannot be modified through the reference but the reference can still be used like a separate object? I'm fairly certain that this is not how it works but then, how does it work? Is there something I missed?
-
Possible duplicate of [What is a constant reference? (not a reference to a constant)](https://stackoverflow.com/questions/7420780/what-is-a-constant-reference-not-a-reference-to-a-constant) – Winter Jun 24 '17 at 14:11
-
6It seems that the weird belief that "passing a constant reference prevents the original object from being modified" has some popularity. No, constant reference does **not** prevents the original object from being modified. It just prevent the object from being modified **through the reference**. – cpplearner Jun 24 '17 at 14:13
-
From an answer of the question I've linked *There are 'const references' which are really 'references to const', that is you can't change the value of the object they refer to.* – Winter Jun 24 '17 at 14:15
-
Possible duplicate of [How C++ reference works](https://stackoverflow.com/q/7418483/608639) – jww Nov 05 '18 at 09:57
3 Answers
I have also learned that passing a constant reference prevents the original object from being modified [...]
Not quite. You are not allowed to modify the object through the const &
. In other words, you have read-only access. But nothing natively prevents other code with read-write access (for example the original owner of the referred object) to modify it. You do need to be careful when designing so that such changes do not surprise you.

- 62,093
- 7
- 131
- 191
-
Thanks for the answer, I didn't realise that constant references are simply read only. I've also updated the question because I understand that the object can be modified by other means. – Gigas Jun 24 '17 at 14:33
A constant reference (const&
) is similar to a pointer to a constant object. You are allowed to read it through the reference but not modify it. Others, holding a non-const reference can still modify it.

- 30,449
- 3
- 47
- 70
Shouldn't a constant reference create a new instance because the original object cannot be modified through the reference but the reference can still be used like a separate object?
It's better to call it a reference to a constant object. This makes it much clearer how the thing works. Calling it the other way around is just confusing because any reference is constant (meaning you can't let it refer to another object after initialization).
So a reference to a constant object is just an additional name for an existing object (like a non-const reference) with the restriction that this name only allows reading from the existing object.
This means that through a reference to a constant object you can:
- only read from member variables of the object, but not assign to them, unless a member is marked as
mutable
- only call methods of the object that are marked as
const
Example:
struct Foo
{
int a;
mutable int b;
void SetA( int newA ) { a = newA; }
int GetA() const { return a; }
};
void DoSomething( const Foo& f )
{
// Here, f is just another name for foo, but it imposes some restrictions:
f.a = 42; // compiler error, can't modify member!
f.SetA( 42 ); // compiler error, can't call non-const method!
int x = f.a; // OK, reading is allowed.
f.b = 42; // OK, because b is marked as mutable
int y = f.GetA(); // OK, because GetA() is marked as const
}
int main()
{
Foo foo;
DoSomething( foo );
}

- 25,437
- 3
- 35
- 72