问题
I decided to see if assigning a reference to a member would make a member a reference. I wrote the following snippet to test it. There's a simple class Wrapper
with an std::string
as a member variable. I take take a const string&
in the constructor and assign it to the public member variable. Later in the main()
method I modify the member variable but the string
I passed to the constructor remains unchanged, how come? I think in Java the variable would have changed, why not in this code snippet? How exactly do references work in this case?
#include <iostream>
#include <string>
using namespace std;
class Wrapper
{
public:
string str;
Wrapper(const string& newStr)
{
str = newStr;
}
};
int main (int argc, char * const argv[])
{
string str = "hello";
cout << str << endl;
Wrapper wrapper(str);
wrapper.str[0] = 'j'; // should change 'hello' to 'jello'
cout << str << endl;
}
回答1:
To assign a reference in a constructor you need to have a reference member
class A{
std::string& str;
public:
A(std::string& str_)
: str(str_) {}
};
str is now a reference to the value you passed in. Same applies for const refs
class A{
const std::string& str;
public:
A(const std::string& str_)
: str(str_) {}
};
However don't forget that once a reference has been assigned it can not be changed so if assignment requires a change to str then it will have to be a pointer instead.
回答2:
Because Wrapper::str
is not a reference, it's an independent object. So when you do str = newStr
, you're copying the string.
回答3:
class Wrapper
{
public:
string& str;
Wrapper(string& newStr) : str(newStr) {}
};
Note, you cannot accept a const string&
and store it in a string&
, you would lose const-correctness in doing so.
回答4:
You need to use an initializer and declare str
as a reference as in:
class Wrapper {
public:
string &str;
Wrapper(string& newStr)
: str(newStr) {
}
};
The way you're writing it, all you are doing is copying the value of the reference you pass to the constuctor. You're not saving the reference. By declaring a reference as a class member and initializing it with a reference to another string instance, you will get the behavior you're looking for.
回答5:
You should declare Wrapper::str
as a string&
, not as a string
.
回答6:
Your main body variable is std::string
.
Your parameter variable is const std::string&
.
The const in references are always "low level const", meaning it modifies the type of object not the actual object.
In contrast a "top level const" modifies an actual object. Read C++ Primer on Top level const for clarification.
Here is how your assignment looks like when you pass arguments:
const std::string& = std::str; //Values are ommited
// i.e const std::string newStr = std::string str
You are initializing a const type reference
with a non-const value
which is acceptable. You are not supposed to change the value of std::string str
using that reference. And, if you try changing the value of newStr
inside the constructor, you will get a compilation error.
Next you're doing another assignment inside the constructor which is also acceptable:
std::string = const std::string
The fact that wrap.str[0]
didn't change str
of main
is that, although a reference was used to instantiate class str
, class str
has its own object and is not linked to main str
. Using that reference in a parameter just links that parameter to main str
; not main str
to class str
.
If your class variables were referenced, then it could have changed.
来源:https://stackoverflow.com/questions/9507008/passing-by-reference-to-a-constructor