In simple terms, a reference is just an alias to the referenced object. When a reference is const
it means you may not modify the object via that reference (the object can be modified via other means, this distinction is important, especially with multithreading). Copying a const
reference is like copying the referenced object:
std::string x = "foo";
const std::string& x_const_ref = x; // constant reference to x
// x can be modifed but not via x_const_ref
//x_const_ref = "moo"; // ERROR !
x = "bar"; // OK !
std::string y = x; // y is initialized with a copy of x
std::string z = x_const_ref; // z is initialized with a copy of x
y
and z
will both hold the string "bar"
, though x
is not modified in the last two lines. And if TCPConnPtr
has no weird copy constructor then thats basically also what happens in your code (if it does try to modify socket
you would get a compiler error).