简体   繁体   English

在继承的模板类中使用下标[]运算符

[英]Using the subscript [] operator in inherited template class

I have a template class Array<T> with the following three member functions defined. 我有一个模板类Array<T> ,其中定义了以下三个成员函数。

template <typename T>
const T& Array<T>::GetElement(int index) const {
    if(index_out_of_bounds(index)) throw OutOfBoundsException(index);
    return m_data[index];
}

template <typename T>
T& Array<T>::operator [] (int index) {
    if(index_out_of_bounds(index)) throw OutOfBoundsException(index);
    return m_data[index];
}

template <typename T>
const T& Array<T>::operator [] (int index) const {
    if(index_out_of_bounds(index)) throw OutOfBoundsException(index);
    return m_data[index];
}

Next I have another template class NumericArray<T> that inherits from Array<T> . 接下来,我还有另一个模板类NumericArray<T> ,它继承自Array<T> This class contains an overloaded operator + . 此类包含重载的+运算符。

template <typename T>
NumericArray<T> NumericArray<T>::operator + (const NumericArray<T> &na) const {
    unsigned int rhs_size = this -> Size(), lhs_size = na.Size();
    if(rhs_size != lhs_size) throw SizeMismatchException(rhs_size, lhs_size);

    NumericArray<T> array_sum(rhs_size);

    for(unsigned int i = 0; i < rhs_size; i++) {
        array_sum[i] = this[i] + na[i];
    }

    return array_sum;
}

Now suppose I instantiate two instances of NumericArray<T> in main.cpp where T is of type int . 现在假设我在main.cpp中实例化了NumericArray<T>两个实例,其中T的类型为int Both instances have already been populated with integer values. 两个实例都已经填充了整数值。

If I now try to perform the + operator I get the following error message: 如果现在尝试执行+运算符,则会收到以下错误消息:

../NumericArray.tpp:44:16: error: cannot convert 'NumericArray' to 'int' in assignment array_sum[i] = this[i] + na[i]; ../NumericArray.tpp:44:16:错误:无法在分配array_sum [i] = this [i] + na [i]中将'NumericArray'转换为'int';

However, if I go back and change the implementation in the for loop of the overloaded operator+ in NumericArray<T> to the following. 但是,如果我返回并将NumericArray<T>重载operator+的for循环中的实现更改为以下内容。 The operator does as expected. 操作员按预期进行。

array_sum[i] = this -> GetElement[i] + na.GetElement[i];

Why is the subscript operator [] not behaving the same if they have equivalent implementations? 如果下标运算符[]具有相同的实现,为什么它们的行为也不相同?

The issue is that you're attempting to apply operator [] on a pointer type: 问题是您试图在指针类型上应用operator []

 for(unsigned int i = 0; i < rhs_size; i++) {
        array_sum[i] = this[i] + na[i];

Since this is a pointer, you must either 由于this是一个指针,因此您必须

1) dereference the pointer first to apply the overloaded operator. 1)首先取消引用指针以应用重载运算符。

or 要么

2) apply the -> operator and use the operator keyword to access the overloaded operator. 2)应用->运算符,并使用operator关键字访问重载的运算符。

Here is an illustration of the two possible solutions: 这是两种可能的解决方案的说明:

    array_sum[i] = (*this)[i] + na[i];

or 要么

    array_sum[i] = this->operator[](i) + na[i];

with the second solution, the this is not necessary: 对于第二种解决方案, this不是必需的:

    array_sum[i] = operator[](i) + na[i];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM