Z
zheng4t
I am learning C++. I found the following code in the book The C++
Programming Language by Bjarne Stroustrup.
struct Pair {
string name;
double val;
};
vector<Pair> pairs;
double& value(const string& s)
{
Pair p = {s, 0};
pairs.push_back(p);
return pairs[pairs.size() - 1].val
}
The push_back takes a reference as parameter. So p would not be
copied. So the return of the function will be a reference to a (part
of) local variable p. What I am missing here?
Programming Language by Bjarne Stroustrup.
struct Pair {
string name;
double val;
};
vector<Pair> pairs;
double& value(const string& s)
{
Pair p = {s, 0};
pairs.push_back(p);
return pairs[pairs.size() - 1].val
}
The push_back takes a reference as parameter. So p would not be
copied. So the return of the function will be a reference to a (part
of) local variable p. What I am missing here?