C++ Overload []

前言

本篇基於Overloading Subscript or array index operator [] in C++,並加上TensorRT中的例子做為參考。

Overload []

以下例子定義了一個類別Array,並對[]這個運算子做overload,使它在進行索引前會先做檢查。

class Array 
{ 
private: 
    int *ptr; 
    int size; 
public: 
    Array(int *p = NULL, int s = 0);
  
    // Overloading [] operator to access elements in array style 
    int& operator[] (int); 
  
    // Utility function to print contents 
    void print() const; 
}; 

// constructor for array class 
Array::Array(int *p, int s) 
{ 
    size = s; 
    ptr = nullptr; 
    if (s != 0) 
    { 
        ptr = new int[s]; 
        for (int i = 0; i < s; i++) 
            ptr[i] = p[i]; 
    } 
} 

// Implementation of [] operator.  This function must return a 
// reference as array element can be put on left side 
int& Array::operator[](int index) 
{ 
    if (index >= size) 
    { 
        cout << "Array index out of bound, exiting" << endl; 
        exit(0); 
    } 
    return ptr[index]; 
} 

void Array::print() const
{ 
    for(int i = 0; i < size; i++) 
        cout<<ptr[i]<<" "; 
    cout<<endl; 
} 

Array類別的使用如下:

int a[] = {1, 2, 4, 5}; 
Array arr1(a, 4); 
arr1[2] = 6; 
arr1.print(); //1 2 6 5
arr1[8] = 6;  //Array index out of bound, exiting

可以看到,當我們使用8去索引時,程序就會報錯並退出。

完整代碼詳見cpp-code-snippets/overload_subscript_operator.cpp

return by reference

注意到在上例中,operator[]的回傳型別為int&而非int。這是為什麼呢?

如果我們試著把operator[]的回傳型別改為int,那麼在編譯時,將會在arr1[2] = 6;這行出現錯誤:

error: expression is not assignable

這是因為在該行代碼中,arr1[2]回傳的是一個右值(詳見C++ lvalue,rvalue及rvalue reference),代表的只是一個暫時的數字,並未指向記憶體的任何地方,卻在這裡被指定為6,這種情況就如同:

4 = 6;

一般荒謬。

如果我們想讓這種寫法成立,就得讓operator[]回傳左值,這也就是[]運算子必須以參考方式回傳的原因。

TensorRT中的例子

TensorRT/parsers/caffe/blobNameToTensor.h中:

class BlobNameToTensor : public IBlobNameToTensor
{
public:
    //...
    nvinfer1::ITensor*& operator[](const std::string& name)
    {
        return mMap[name];
    }
    //...
private:
    std::map<std::string, nvinfer1::ITensor*> mMap;
    //...
};

overload了operator[],讓使用者能直接對BlobNameToTensor型別的物件使用[]來存取mMap這個map。

值得注意的是,operator[]回傳值的型別為nvinfer1::ITensor*&,這代表它回傳的是map裡value的參考,為一左值。

參考連結

Overloading Subscript or array index operator [] in C++

C++ lvalue,rvalue及rvalue reference

cpp-code-snippets/overload_subscript_operator.cpp

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值