简体   繁体   English

通过引用传递给构造函数

[英]Passing by reference to a constructor

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. 有一个简单的类Wrapper ,其中std::string作为成员变量。 I take take a const string& in the constructor and assign it to the public member variable. 我在构造const string&取一个const string&并将其分配给public成员变量。 Later in the main() method I modify the member variable but the string I passed to the constructor remains unchanged, how come? 后来在main()方法中我修改了成员变量但是我传递给构造函数的string保持不变,怎么样? I think in Java the variable would have changed, why not in this code snippet? 我认为在Java中变量会发生变化,为什么不在这段代码中呢? 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;
}

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引用

 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,那么它必须是指针。

Because Wrapper::str is not a reference, it's an independent object. 因为Wrapper::str不是引用,所以它是一个独立的对象。 So when you do str = newStr , you're copying the string. 所以当你执行str = newStr ,你正在复制字符串。

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. 注意,你不能接受一个const string&并将它存储在一个string& ,这样做会失去const-correctness。

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. 通过将引用声明为类成员并使用对另一个字符串实例的引用对其进行初始化,您将获得您正在寻找的行为。

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. 引用中的const始终是“低级别const”,这意味着它修改了对象的类型而不是实际对象。

In contrast a "top level const" modifies an actual object. 相比之下,“顶级const”修改了一个实际的对象。 Read C++ Primer on Top level const for clarification. 阅读顶级const上的C ++ Primer以获得澄清。

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. 您正在使用可接受的non-const value初始化const type reference You are not supposed to change the value of std::string str using that reference. 您不应该使用该引用更改std::string str的值。 And, if you try changing the value of newStr inside the constructor, you will get a compilation error . 并且,如果您尝试在构造函数中更改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 . 这一事实wrap.str[0]不改变strmain是,虽然参考了用于实例化class strclass str有它自己的对象和未链接到main str Using that reference in a parameter just links that parameter to main str ; 在参数中使用该引用只是将该参数链接到main str ; not main str to class str . 不是main str class str

If your class variables were referenced, then it could have changed. 如果引用了类变量,那么它可能已经改变了。

您应该将Wrapper::str声明为string&而不是string

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM