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

时间:2022-12-21 05:36:57

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 main that calls it:

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

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'.

我得到一个编译器警告:错误:从'const int'类型的表达式中无效初始化'int&'类型的引用。

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

那是什么呢?我传入一个int类型。难道它不应该自动地变成我的财产吗?为什么编译器会抱怨呢?

2 个解决方案

#1


12  

obj is a const reference. object is a non-const reference.

obj是一个常量引用。对象是一个非常量引用。

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引用,因为这样做将首先挫败具有const引用的目的。

If you want your instance of Object to be able to modify the int that'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.

如果您希望对象的实例能够修改传递给其构造函数的int,那么构造函数应该采用非const引用。如果没有,那么数据成员应该是const引用。

In any case, you are storing up trouble for yourself if you use new to allocate objects that have references as data members. It's your problem to ensure that you delete the Object before i goes out of scope (or anyway, ensure that the Object doesn't use its member object after i goes out of scope.

无论如何,如果您使用new来分配作为数据成员具有引用的对象,那么您将为自己存储麻烦。确保在我超出范围之前删除对象是您的问题(或者无论如何,确保在我超出范围之后,对象不会使用它的成员对象)。

#2


8  

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

您试图将const引用分配给非const引用。这意味着您的对象类可以修改对象的内容。

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

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

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

不要让你这么做。您可以复制对象或将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 can't modify object

在这种情况下,您不能修改对象

#1


12  

obj is a const reference. object is a non-const reference.

obj是一个常量引用。对象是一个非常量引用。

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引用,因为这样做将首先挫败具有const引用的目的。

If you want your instance of Object to be able to modify the int that'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.

如果您希望对象的实例能够修改传递给其构造函数的int,那么构造函数应该采用非const引用。如果没有,那么数据成员应该是const引用。

In any case, you are storing up trouble for yourself if you use new to allocate objects that have references as data members. It's your problem to ensure that you delete the Object before i goes out of scope (or anyway, ensure that the Object doesn't use its member object after i goes out of scope.

无论如何,如果您使用new来分配作为数据成员具有引用的对象,那么您将为自己存储麻烦。确保在我超出范围之前删除对象是您的问题(或者无论如何,确保在我超出范围之后,对象不会使用它的成员对象)。

#2


8  

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

您试图将const引用分配给非const引用。这意味着您的对象类可以修改对象的内容。

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

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

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

不要让你这么做。您可以复制对象或将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 can't modify object

在这种情况下,您不能修改对象