在 C++ 中将成员的引用初始化为 NULL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9055778/
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
Initializing a reference to member to NULL in C++
提问by Idov
Is it possible to initialize a reference member to NULL in c++?
I'm trying to something like this:
是否可以在 C++ 中将引用成员初始化为 NULL?
我正在尝试这样的事情:
class BigClass
{
private:
Object m_inner;
public:
const Object& ReadOnly;
BigClass() : ReadOnly(NULL)
{
Do stuff.
}
};
I know I can do this if I initialize "ReadOnly" to a real reference of an object, but when I want to put in there "NULL", i get the error:
我知道如果我将“ReadOnly”初始化为对象的真实引用,我可以做到这一点,但是当我想在那里放入“NULL”时,我收到错误消息:
"cannot convert from 'int' to 'const Object &'
“无法从‘int’转换为‘const Object &’
How can I solve this?
我该如何解决这个问题?
回答by Oliver Charlesworth
No, references cannot be NULL
in C++.1
不,引用不能NULL
在 C++ 中。1
Possible solutions include:
可能的解决方案包括:
- using a pointer instead of a reference.
- having a dummy
Object
instance that can be used to indicate "no object".
- 使用指针而不是引用。
- 具有
Object
可用于指示“无对象”的虚拟实例。
[1] From the C++11 standard:
[1] 来自C++11 标准:
[dcl.ref][...] a null reference cannot exist in a well-defined program, because the only way to create such a reference would be to bind it to the “object” obtained by dereferencing a null pointer, which causes undefined behavior.
[dcl.ref][...] 在定义良好的程序中不能存在空引用,因为创建这种引用的唯一方法是将其绑定到通过取消引用空指针获得的“对象”,这会导致未定义的行为。
回答by Mat
You cannot "solve" this. Use a pointer if you want to be able to have that member not point to anything.
你不能“解决”这个问题。如果您希望该成员不指向任何内容,请使用指针。
References must be initialized to a real object, they cannot "point nowhere".
引用必须初始化为一个真实的对象,它们不能“无处指向”。
回答by Dietmar Kühl
It can be done but it is almost certainly an extremely bad idea. The way to do it is to dereference a suitably typed NULL
pointer which already shows that it is a bad idea: you arrive at undefined behavior at this point which, however, typically tends to "work".
这是可以做到的,但几乎可以肯定这是一个非常糟糕的主意。这样做的方法是取消引用一个适当类型的NULL
指针,这已经表明这是一个坏主意:此时您会遇到未定义的行为,但通常倾向于“工作”。
In C++ references are meant to always refer to an actual object. This is different to other programming languages where "references" are actually the equivalent of pointersin C++ (typically without things like pointer arithmetic). What you probably actually want (you unfortunately didn't say what you try to achieve it but asked about a solution to a problem which is probably part of a misguided approach) is to use a pointer instead:
在 C++ 中,引用旨在始终引用实际对象。这与其他编程语言不同,在其他编程语言中,“引用”实际上相当于C++ 中的指针(通常没有指针算术之类的东西)。您可能真正想要的(不幸的是,您没有说明您尝试实现的目标,而是询问了可能是误导方法的一部分的问题的解决方案)是使用指针:
Object const* const readOnly;
BigClass(): readOnly(0) {}
回答by QuentinUK
Use a pointer:- const Object* pReadOnly;
使用指针:- const Object* pReadOnly;
回答by cdunn2001
It's useful in writing unit-tests. That is the onlyplace it should be done, but there, it's quite helpful.
它在编写单元测试时很有用。这是唯一应该做的地方,但在那里,它很有帮助。
Bar& bar(*static_cast<Bar*>(0));
MockClass mock; // derives from RealClass
mock.foo(bar);
Here, I am testing code which uses MockClass
, not MockClass
itself.
在这里,我正在测试使用 的代码MockClass
,而不是MockClass
它本身。
It's not a panacea, but it can help. Also, GoogleMockmight be your friend if you are mocking "concrete" classes.
这不是万能药,但可以提供帮助。此外,如果您正在模拟“具体”类,GoogleMock可能是您的朋友。
struct Bar;
struct RealClass {
int& x_;
double& y_;
RealClass(int& x, double& y) :x_(x), y_(y) {}
virtual void foo(Bar&);
};
struct MockClass: public RealClass {
MockClass(): RealClass(*(int*)0, *(double*)0) {}
MOCK_METHOD1(foo, void(Bar&));
};