C++ 通过引用传递给构造函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9507008/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing by reference to a constructor
提问by rcplusplus
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?
我决定看看为成员分配引用是否会使成员成为引用。我写了下面的代码片段来测试它。有一个简单的类Wrapper
,std::string
作为成员变量。我const string&
在构造函数中取 a并将其分配给公共成员变量。后来在main()
方法中我修改了成员变量,但是string
我传递给构造函数的却没有改变,这是怎么回事?我认为在 Java 中变量会改变,为什么不在这个代码片段中?在这种情况下,引用究竟是如何工作的?
#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;
}
回答by 111111
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
str 现在是对您传入的值的引用。同样适用于 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.
但是不要忘记,一旦分配了引用,就无法更改它,因此如果分配需要更改 str ,则它必须是指针。
回答by Oliver Charlesworth
Because Wrapper::str
is not a reference, it's an independent object. So when you do str = newStr
, you're copyingthe string.
因为Wrapper::str
不是引用,它是一个独立的对象。所以当你这样做时str = newStr
,你正在复制字符串。
回答by netcoder
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.
请注意,您不能接受 aconst string&
并将其存储在 a 中string&
,这样做会失去常量正确性。
回答by andand
You need to use an initializer and declare str
as a reference as in:
您需要使用初始化程序并声明str
为引用,如下所示:
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.
你写它的方式,你所做的就是复制你传递给构造函数的引用的值。你没有保存参考。通过将引用声明为类成员并使用对另一个字符串实例的引用对其进行初始化,您将获得所需的行为。
回答by Mikhail
You should declare Wrapper::str
as a string&
, not as a string
.
您应该声明Wrapper::str
为 a string&
,而不是 a string
。
回答by Jesus Christ
Your main body variable is std::string
.
您的主体变量是std::string
.
Your parameter variable is const std::string&
.
您的参数变量是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 constfor clarification.
相反,“顶级常量”修改实际对象。阅读C++ Primer on Top level const进行澄清。
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.
您正在const type reference
使用non-const value
可接受的a初始化 a 。您不应该更改std::string str
使用该引用的值。而且,如果您尝试更改newStr
构造函数内部的值,您将收到编译错误。
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
.
这一事实wrap.str[0]
并没有改变str
的main
是,尽管参考使用实例化class str
,class str
有其自己的对象,而不是挂main str
。在参数中使用该引用只是将该参数链接到main str
; 不main str
给class str
。
If your class variables were referenced, then it could have changed.
如果您的类变量被引用,那么它可能已经改变。