有错误的引用变量,必须在构造函数的base / member初始化程序中初始化 [英] Reference variable with error, must be initialized in constructor 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_' :必须在构造函数base / 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;
}
推荐答案
首先,您的 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
构造函数结束时 die ?您确定引用是您所需要的吗?
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 A
constructor 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)
构造函数创建的,但是提供的字符串位于全局范围内,因此它不会会被销毁,并且 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 ::给定对象的字符串
!那是你想要的吗?
The copied object value will reference the std::string
of the given object! Is that what you want?
这篇关于有错误的引用变量,必须在构造函数的base / member初始化程序中初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!