引用varable有错误,必须在构造函数base / member initializer中初始化 [英] Reference varable with error, must be initialized in constructor base/member initializer

查看:173
本文介绍了引用varable有错误,必须在构造函数base / member initializer中初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试编译下面的源代码时,我得到以下错误。

I got following error when I try to compile the source-code below. Can anybody describe why this error exists and how I can fix it?

错误1错误C2758:'A :: s_':必须在构造函数库中初始化/ member initializer

#include <iostream>
#include <string>

using namespace std;

class A
{
public:
    A(string& s) : s_(s) { cout << "A::ctor" << endl; }
    A(const A& rhs)      { cout << "A::copy" << endl; }
    ~A()                 { cout << "A::dtor" << endl; }

    A& operator=(const A& rhs) { cout << "A::copyassign" << endl; }

private:
    string& s_;    
};

int main()
{

    return 0;
}


推荐答案

code> A :: s _ 是对 std :: string 的引用;这意味着它引用的东西必须存在于某处。

First of all, your A::s_ is a reference to a std::string; that means that it's referencing something that must exists somewhere.

由于他的引用类型,以及引用必须在创建时初始化的事实,你必须在所有 A 构造函数(由其他用户指定)中初始化 A :: s _

Due of his reference type, and the fact that the references must be initialized at the moment they're created, you must initialize A::s_ in ALL the A constructors (as pointed by other users):

class A
{
public:
    A(string& s) : s_(s)
    { cout << "A::ctor" << endl; }

    A(const A& rhs) : s_(rhs.s_) // <-- here too!!
    { cout << "A::copy" << endl; }

    ~A()
    { cout << "A::dtor" << endl; }

    A& operator=(const A& rhs)
    { cout << "A::copyassign" << endl; }

private:
    string& s_;    
};

现在,回到我提到的第一件事: A :: s _ 必须引用存在的内容,因此您必须注意一些事情,请看下面的代码:

And now, back to the first thing I mentioned; the A::s_ must reference something that exists, so you must be aware of some things, take a look at the following code:

int main()
{
    // New A instance:
    A a("hello world");

    return 0;
}

构建此 A 实例我们提供了一个 const char [12] 值,这个值是一个临时 std :: string 并且被赋予 A :: A(string& s)构造函数。其中 A :: s _ 是在构造函数结束后引用的?创建的临时 std :: string 会发生什么?当 A 构造函数结束时,它的生命周期是延长还是只是 您确定参考是您需要的吗?

Constructing this A instance we're providing a const char[12] value, with this value a temporary std::string is created and is given to the A::A(string& s) constructor. Where A::s_ is referencing after the constructor ends? What happens with the temporary std::string created? It's lifetime is extended or it just die when the Aconstructor ends? Are you sure that a reference is what you need?

std::string s("hello world");

int main()
{
    // New A instance:
    A a(s);

    return 0;
}

使用上面的代码,新的 A 创建的实例调用相同的 A :: A(string& s)构造函数,但是提供的字符串位于全局作用域中, t被销毁,并且 a 实例中的 A :: s _ 将引用一个有效的字符串,真正的威胁是在复制构造函数中:

With the code above, a new A instance is created calling the same A::A(string& s) constructor, but with a provided string lying in the global scope, so it doesn't be destroyed and the A::s_ from the a instance would reference a valid string all its lifetime, but the real threat is in the copy constructor:

std::string s("hello world");

int main()
{
    A a(s);    // a.s_ references the global s.
    A b(a);    // b.s_ references the a.s_ that references the global s.

    return 0;
}

复制的对象值将引用 std :: string !这是你想要的吗?

The copied object value will reference the std::string of the given object! Is that what you want?

这篇关于引用varable有错误,必须在构造函数base / member initializer中初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆