Accessing the [] operator from a pointer

前端 未结 7 594
北海茫月
北海茫月 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:26

    You could do any of the following:

    #include <vector>
    
    int main () {
      std::vector<int> v(1,1);
      std::vector<int>* p = &v;
    
      p->operator[](0);
      (*p)[0];
      p[0][0];
    }
    

    By the way, in the particular case of std::vector, you might also choose: p->at(0), even though it has a slightly different meaning.

    0 讨论(0)
  • 2020-11-30 03:29
    return VecPtr->operator[](0);
    

    ...will do the trick. But really, the (*VecPtr)[0] form looks nicer, doesn't it?

    0 讨论(0)
  • 2020-11-30 03:30

    You can use it as VecPrt->operator [] ( 0 ), but I'm not sure you'll find it less obscure.

    0 讨论(0)
  • 2020-11-30 03:36

    (*VecPtr)[0] is perfectly OK, but you can use the at function if you want:

    VecPtr->at(0);
    

    Keep in mind that this (unlike operator[]) will throw an std::out_of_range exception if the index is not in range.

    0 讨论(0)
  • 2020-11-30 03:44

    People are advising you to use ->at(0) because of range checking. But here is my advise (with other point of view):

    NEVER use ->at(0)! It is really slower. Would you sacrifice performance just because you are lazy enough to not check range by yourself? If so, you should not be programming in C++.

    I think (*VecPtr)[0] is ok.

    0 讨论(0)
  • 2020-11-30 03:46

    There's another way, you can use a reference to the object:

    #include <iostream>
    #include <vector>
    using namespace std;
    
    int main()
    {
        vector<int> v = {7};
        vector<int> *p = &v;
    
        // Reference to the vector
        vector<int> &r = *p;
        cout << (*p)[0] << '\n'; // Prints 7
        cout <<    r[0] << '\n'; // Prints 7
    
        return 0;
    }
    

    This way, r is the same as v and you can substitute all occurrences of (*p) by r.

    Caveat: This will only work if you won't modify the pointer (i.e. change which object it points to).

    Consider the following:

    #include <iostream>
    #include <vector>
    using namespace std;
    
    int main()
    {
        vector<int> v = {7};
        vector<int> *p = &v;
    
        // Reference to the vector
        vector<int> &r = *p;
        cout << (*p)[0] << '\n'; // Prints 7
        cout <<    r[0] << '\n'; // Prints 7
    
        // Caveat: When you change p, r is still the old *p (i.e. v)
        vector<int> u = {3};
        p = &u; // Doesn't change who r references
        //r = u; // Wrong, see below why
        cout << (*p)[0] << '\n'; // Prints 3
        cout <<    r[0] << '\n'; // Prints 7
    
        return 0;
    }
    

    r = u; is wrong because you can't change references: This will modify the vector referenced by r (v) instead of referencing another vector (u). So, again, this only works if the pointer won't change while still using the reference.

    The examples need C++11 only because of vector<int> ... = {...};

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