C++ 错误:从“const int”类型的表达式初始化“int&”类型的引用无效

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11371216/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 15:05:27  来源:igfitidea点击:

error: invalid initialization of reference of type 'int&' from expression of type 'const int'

c++compiler-errors

提问by Michael Dorst

This is an unrelated question about the code in this question, regarding the following template function.

这是一个与此问题中的代码无关的问题,关于以下模板函数。

template <class T>
class Object : public Container {
public:
    T& object;

    Object(const T& obj) : object(obj) {}
};

This is the code that calls the constructor:

这是调用构造函数的代码:

template <class T>
void Array::add_element(const T& element)
{
    vec.push_back(new Object<T>(element));
}

This code compiles fine, but as soon as I add a line in mainthat calls it:

这段代码编译得很好,但只要我在main其中添加一行调用它:

Array array;
int i = 3;
array.add_element(i);

I get a compiler warning: error: invalid initialization of reference of type 'int&' from expression of type 'const int'.

我收到编译器警告:error: invalid initialization of reference of type 'int&' from expression of type 'const int'.

What is that about? I passed an intin. Shouldn't it automatically be turned into a const int&for me? Why is the compiler complaining?

那是关于什么的?我传int进去了const int&,不应该自动变成我的吗?为什么编译器会抱怨?

回答by Steve Jessop

objis a const reference. objectis a non-const reference.

obj是一个常量引用。object是一个非常量引用。

You can't initialize a non-const reference from a const reference, because doing so would defeat the purpose of having a const reference in the first place.

您不能从 const 引用初始化非常量引用,因为这样做会破坏首先拥有 const 引用的目的。

If you want your instance of Objectto be able to modify the intthat's passed to its constructor, then the constructor should take a non-const reference. If you don't, then the data member should be a const reference.

如果您希望您的实例Object能够修改int传递给其构造函数的 ,那么构造函数应该采用非常量引用。如果不这样做,那么数据成员应该是一个常量引用。

In any case, you are storing up trouble for yourself if you use newto allocate objects that have references as data members. It's yourproblem to ensure that you delete the Objectbefore igoes out of scope (or anyway, ensure that the Objectdoesn't use its member objectafter igoes out of scope.

在任何情况下,如果您使用new分配具有引用作为数据成员的对象,您就会为自己存储麻烦。确保您删除before超出范围是您的问题(或者无论如何,确保在超出范围之后不使用其成员。ObjectiObjectobjecti

回答by ilmale

You are trying to assign a const reference to a non const reference. This means that your Object class can modify the content of object.

您正在尝试将常量引用分配给非常量引用。这意味着您的 Object 类可以修改 object 的内容。

const int myConstNumber = 4;
Object<int> intObj(myConstNumber);

intObj.object = 3; // you have indirectly modified the value of myConstNumber

C++ doesn't let you do that. You can make a copy of the object or add the const to your attribute.

C++ 不允许你这样做。您可以制作对象的副本或将 const 添加到您的属性中。

template <class T>
class Object : public Container {
public:
    T object; // valid

or

或者

template <class T>
class Object : public Container {
public:
    const T& object; // valid

in this case you won't be able to modify object

在这种情况下,您将无法修改对象