Accessing the [] operator from a pointer

前端 未结 7 616
北海茫月
北海茫月 2020-11-30 02:52

If I define a pointer to an object that defines the [] operator, is there a direct way to access this operator from a pointer?

For example, in the follo

相关标签:
7条回答
  • 2020-11-30 03:49

    It is worth noting that in C++11 std::vector has a member function 'data' that returns a pointer to the underlying array (both const and non-const versions), allowing you to write the following:

    VecPtr->data()[0];
    

    This might be an alternative to

    VecPtr->at(0);
    

    which incurs a small runtime overhead, but more importantly it's use implies you aren't checking the index for validity before calling it, which is not true in your particular example.

    See std::vector::data for more details.

    0 讨论(0)
提交回复
热议问题