Reputation: 93
Very simple question, which most likely calls for an explanation on how references work and why my understanding is flawed.
Given the simple code snippet below:
#include <iostream>
#include <string>
struct Foo
{
std::string &ref;
Foo(std::string &bar) : ref(bar) { }
};
int main()
{
std::string s1 = "foo1";
Foo f(s1);
f.ref[0] = 'b';
std::cout << s1 << std::endl; // prints boo1
{
std::string f2("tmp");
f.ref = f2;
}
// shouldn't f.ref be dangling by now?
std::cout << f.ref; // prints tmp
}
Output:
My understanding is that f2 will be destroyed at the end of that block, thus f.ref will be a dangling reference. What is really going on?
Upvotes: 1
Views: 95
Reputation: 141574
f.ref = f2
is the same as s1 = s2
, because f.ref
and s1
are both names for the same string
object.
Perhaps you are mixing up the concepts of initialization of a reference, and assignment of objects. A reference can only be initialized once (and in fact, must be initialized on creation).
In the initialization ref(bar)
, ref
is initialized to refer to the same object bar
refers to (which is also the same object as is called s1
).
In the assignment expression f.ref = f2;
the assignment operator is called for the object which f.ref
names.
Upvotes: 3