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 following code I can directly access Vec
's member functions (such as empty()
) by using the pointer's ->
operator, but if I want to access the []
operator I need to first get a reference to the object and then call the operator.
#include <vector>
int main(int argc, char *argv[])
{
std::vector<int> Vec(1,1);
std::vector<int>* VecPtr = &Vec;
if(!VecPtr->empty()) // this is fine
return (*VecPtr)[0]; // is there some sort of ->[] operator I could use?
return 0;
}
I might very well be wrong, but it looks like doing (*VecPtr).empty()
is less efficient than doing VecPtr->empty()
. Which is why I was looking for an alternative to (*VecPtr)[]
.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…