c++ - &member 到 const 成员

标签 c++ operator-overloading

我遇到了一些错误。我需要使用运算符 = 将成员传递给 const 成员。 我不知道错误是什么,我的函数声明是:

template<class I> forceinline
void OverweightValues<I>::init(int t,
                        SharedArray<int>& elements0,
                        SharedArray<int>& weights0,
                        I& i) {
{..}
//both are declared as 'const SharedArray<int> elements, weights;'
//but elements0 and weights0 not is const
elements = elements0; 
weights = weights0;

编译时报错:

In line(16): no operator "=" matches these operands operand types are: const SharedArray<int> = SharedArray<int>
In line(17): no operator "=" matches these operands operand types are: const SharedArray<int> = SharedArray<int>    

我该如何解决这个问题?

最佳答案

const 成员只能在创建时设置。您需要使用构造函数初始化列表而不是两阶段构造。 (即使是构造函数体也不能赋值)

关于c++ - &member 到 const 成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29312670/

相关文章:

c++ - 以下语句是函数重载还是函数偏特化?

c++ - 与 2012 年相比,申请不断关闭

c++ - ==<> 运算符的含义是什么?

C#:隐式运算符和扩展方法

c++ - 如何正确地使用 GLFW 进行上下文共享?

c++模板子类调用错误的父构造函数

c++ - 如何迭代 std::tuple 的元素?

c++ - if(!(is >> s)) 是什么意思?

c++ - 为什么我不能在运算符重载中使用两个指针?

c++ - 重载 operator<< 以作为成员打印