小编Jac*_*ink的帖子

C++ - 基于赋值方重载[]运算符

我正在尝试用c ++编写动态数组模板

我目前正在重载[]运算符,我想根据它们使用的赋值方面实现不同的行为.

#include <iostream>
...

template <class T>
T dynamic_array<T>::operator[](int idx) {
    return this->array[idx];
}

template <class T>
T& dynamic_array<T>::operator[](int idx) {
    return this->array[idx];
}

using namespace std;
int main() {
    dynamic_array<int>* temp = new dynamic_array<int>();

    // Uses the T& type since we are explicitly 
    // trying to modify the stored object
    (*temp)[0] = 1;

    // Uses the T type since nothing in the array 
    // should be modified outside the array
    int& b = (*temp)[0]; 

    // For instance... …
Run Code Online (Sandbox Code Playgroud)

c++ operator-overloading

10
推荐指数
2
解决办法
1267
查看次数

标签 统计

c++ ×1

operator-overloading ×1