Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there other syntax for this pointer operation?

Tags:

c++

syntax

This is a really basic question, but I'm not sure how to google it.

I have a pointer to a vector (or array of pointers), say

vector<int *> *p;

Is there an alternative syntax for indexing this array, other than

(*p)[i];

akin to the -> operator?

like image 822
Eric Tressler Avatar asked Sep 09 '26 14:09

Eric Tressler


2 Answers

p->at(i) is similar but does bounds checking and throws an exception if i is out of range. Looks nicer than both your and Luchian's solutions (IMO), and is a little safer.

Yes -

p->operator[](i);

but it's not really better if you ask me.

like image 41
Luchian Grigore Avatar answered Sep 11 '26 04:09

Luchian Grigore