如何在C ++中初始化对象引用?

时间:2022-02-02 16:54:20

I tried to do

我试着这样做

MyClass& x;
x = MyClass(a,b,c);

But C++ won't let me do so because it thinks that x is uninitialized at the beginning.

但是C ++不会让我这样做,因为它认为x在开始时是未初始化的。

So I tried to do

所以我试着这样做

MyClass& x = MyClass(a,b,c);

But got error saying invalid initialization of non-const reference of type 'MyClass&' from an rvalue of type 'MyClass'

但得到的错误是从'MyClass'类型的右值开始无效初始化'MyClass&'类型的非const引用

What's wrong with it? It seems that I simply can't do anything now. How do I get around the initialization issue?

它出什么问题了?看来我现在根本无能为力。如何解决初始化问题?

2 个解决方案

#1


6  

A reference must refer to an already-existing object. So you need to have an object first before you can refer to it.

引用必须引用已存在的对象。所以你需要先拥有一个对象才能引用它。

MyClass y = MyClass(a,b,c);
MyClass &x = y;

#2


9  

An ordinary reference to non-const must be initialized with an lvalue expression (essentially an expression that refers to a memory location), e.g.

对非const的普通引用必须用左值表达式(本质上是指代存储器位置的表达式)初始化,例如,

MyClass o{ a, b, c };
MyClass& r = o;

If the reference is to const, or if it is an rvalue reference (denoted by &&), then the initializer can instead be an rvalue expression, such as a temporary produced by a function invocation:

如果引用是const,或者它是rvalue引用(用&&表示),那么初始化器可以是rvalue表达式,例如函数调用产生的临时表达式:

MyClass const& rc = foo();
MyClass&& rr = foo();

In these cases, for a local reference the lifetime of the temporary is extended to the scope of the reference.

在这些情况下,对于本地引用,临时的生命周期将扩展到引用的范围。

And one special feature is that if the initializer produces a temporary of a derived class, it's that full derived class object whose lifetime is extended, i.e. there's no slicing to the class specified for the reference.

还有一个特殊功能是,如果初始化程序生成一个派生类的临时类,那就是完整的派生类对象,它的生命周期被扩展,即对于为引用指定的类没有切片。

More generally the reference can be bound to any part of a temporary, as long as that part has a compatible type, and this will extend the lifetime of the full temporary object.

更一般地,引用可以绑定到临时的任何部分,只要该部分具有兼容类型,这将延长完整临时对象的生命周期。

#1


6  

A reference must refer to an already-existing object. So you need to have an object first before you can refer to it.

引用必须引用已存在的对象。所以你需要先拥有一个对象才能引用它。

MyClass y = MyClass(a,b,c);
MyClass &x = y;

#2


9  

An ordinary reference to non-const must be initialized with an lvalue expression (essentially an expression that refers to a memory location), e.g.

对非const的普通引用必须用左值表达式(本质上是指代存储器位置的表达式)初始化,例如,

MyClass o{ a, b, c };
MyClass& r = o;

If the reference is to const, or if it is an rvalue reference (denoted by &&), then the initializer can instead be an rvalue expression, such as a temporary produced by a function invocation:

如果引用是const,或者它是rvalue引用(用&&表示),那么初始化器可以是rvalue表达式,例如函数调用产生的临时表达式:

MyClass const& rc = foo();
MyClass&& rr = foo();

In these cases, for a local reference the lifetime of the temporary is extended to the scope of the reference.

在这些情况下,对于本地引用,临时的生命周期将扩展到引用的范围。

And one special feature is that if the initializer produces a temporary of a derived class, it's that full derived class object whose lifetime is extended, i.e. there's no slicing to the class specified for the reference.

还有一个特殊功能是,如果初始化程序生成一个派生类的临时类,那就是完整的派生类对象,它的生命周期被扩展,即对于为引用指定的类没有切片。

More generally the reference can be bound to any part of a temporary, as long as that part has a compatible type, and this will extend the lifetime of the full temporary object.

更一般地,引用可以绑定到临时的任何部分,只要该部分具有兼容类型,这将延长完整临时对象的生命周期。