引用成员的初始化需要一个临时变量 C++
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1701416/
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
Initialization of reference member requires a temporary variable C++
提问by aajkaltak
struct Div
{
int i;
int j;
};
class A
{
public:
A();
Div& divs;
};
In my constructor definition, I have the following
在我的构造函数定义中,我有以下内容
A::A() : divs(NULL)
{}
I get the following error:
我收到以下错误:
Error72 error C2354:
'A::divs' : initialization of reference member requires a temporary variable
回答by Mike Seymour
A reference must be initialised to refer to something; it can't refer to nothing, so you can't default-construct a class that contains one (unless, as others suggest, you define a global "null" value). You will need a constructor that is given the Div
to refer to:
必须初始化一个引用来引用某物;它不能引用任何内容,因此您不能默认构造一个包含一个类的类(除非像其他人建议的那样,您定义了一个全局“空”值)。您将需要一个构造函数Div
来引用:
explicit A(Div &d) : divs(d) {}
If you want it to be able to be "null", then you need a pointer, not a reference.
如果您希望它能够为“空”,那么您需要一个指针,而不是一个引用。
回答by Russell Newquist
divs is a reference, not a pointer. You can't set it to NULL, it has to point to an actual object of some kind. The best thing to do here is probably to define a static/global instance of Div that you arbitrarily define to be the "Null Div" (set its value to something you're unlikely ever to use) and initialize div to that. Something like this:
divs 是一个引用,而不是一个指针。您不能将其设置为 NULL,它必须指向某种实际对象。这里最好的做法可能是定义一个 Div 的静态/全局实例,您可以将其任意定义为“Null Div”(将其值设置为您不太可能使用的值)并将 div 初始化为该实例。像这样的东西:
struct Div
{
int i;
int j;
};
Div NULL_DIV = { INT_MAX, INT_MAX };
class A
{
public:
A();
Div& divs;
};
A::A() : divs(NULL_DIVS)
{
}
Or, alternatively, just make divs a pointer instead of a reference.
或者,也可以将 div 设为指针而不是引用。
*Note that you can't use a const reference unless you cast away the constness because by default the compiler won't allow you to assign a cosnt ref to a non-const ref.
*请注意,除非放弃常量性,否则不能使用常量引用,因为默认情况下编译器不允许您将 cosnt 引用分配给非常量引用。
回答by jldupont
For one, you can't have a NULL reference. As second, all variable references in a class must be initialized at construction time.
一方面,您不能拥有 NULL 引用。其次,类中的所有变量引用都必须在构造时初始化。
回答by Abel
In "English": a reference refers to something. It cannot refer to nothing(null). That's why references are safer to use then pointers.
在“英语”中:引用是指某事。它不能引用任何内容(空值)。这就是为什么引用比使用指针更安全的原因。
回答by jalf
References have to reference something. There is no such thing as a null reference in the C++ language. If the member may not have a value, then it should be a pointer, or a boost::optional
or some type like that.
引用必须引用一些东西。C++ 语言中没有空引用这样的东西。如果成员可能没有值,那么它应该是一个指针,或者boost::optional
类似的类型。
A reference must be initialized to reference a valid object.
必须初始化引用以引用有效对象。
回答by qid
Keep in mind that once a reference is initialized to point to something, you cannot alter it to point to something else. If this is not the desired behavior, then you should use a pointer or a copy of the object instead.
请记住,一旦引用被初始化为指向某事物,您就无法将其更改为指向其他事物。如果这不是所需的行为,那么您应该改用指针或对象的副本。
回答by AnT
The compiler message is one of those messages that don't make sense from the language point of view, but reveal the inner workings of the compiler, the sequence in which its inner logic works.
编译器消息是从语言的角度来看没有意义的消息之一,但揭示了编译器的内部工作,其内部逻辑工作的顺序。
In your case you are using an rvalue (NULL
) to initialize a reference. When such initialization is allowed, the rvalue is converted to a temporary object, to which the reference will be bound. So, the compiler has realized it right away and informed you about the fact with a diagnostic message.
在您的情况下,您使用右值 ( NULL
) 来初始化引用。当允许这样的初始化时,右值被转换为一个临时对象,引用将被绑定到该对象。因此,编译器立即意识到了这一点,并通过诊断消息告知您这一事实。
In reality though, the trick like that is only allowed for const
references, so your code is broken, since the reference is not const
in our case. Also, a struct
reference, as the one in your code, cannot be initialized with NULL
rvalue (which has integral type), so it is broken for that reason as well.
但实际上,这样的技巧只允许用于const
引用,所以你的代码被破坏了,因为引用不在const
我们的例子中。此外,struct
作为代码中的引用,不能用NULL
右值(具有整数类型)初始化,因此它也因此而被破坏。
The compiler's message is rather misleading though. The text of the message seems to imply that it is illegal to initialize member references with temporary objects. In fact, this is legal in C++ (once the above problems are fixed), although it makes no sense. But, I guess, once ill-formed code is accompanied by at least some error message, it should be OK for everyone...
不过,编译器的消息相当具有误导性。消息文本似乎暗示用临时对象初始化成员引用是非法的。事实上,这在 C++ 中是合法的(一旦上述问题被修复),尽管它没有意义。但是,我想,一旦格式错误的代码至少伴随着一些错误消息,对每个人来说都应该没问题......
回答by Marcin
Plain and simple:
干净利落:
A reference can never be NULL.
引用永远不能为 NULL。
回答by konstantin314
class A
{
Div & ref(Div div) { return div; }
public:
A() : divs(ref(Div())) {};
Div& divs;
};
回答by Phillip Ngan
As noted in other posts, references (Div&) cannot be null. So the most straightforward change you can make is to provide a reference in the constructor and initialize your reference member. Like this,
如其他帖子所述,引用 (Div&) 不能为空。因此,您可以进行的最直接的更改是在构造函数中提供一个引用并初始化您的引用成员。像这样,
class A
{
public:
A(Div& inDivs);
Div& divs;
};
public A::A( Div& inDivs )
: divs( inDivs )
{}