0

I don't understand this code:

T& operator[](int index);
        const T& operator[](int index) const;

And, declared in the private is this:

T *ptr;  

What does this mean?

3
Contributors
3
Replies
25
Views
4 Years
Discussion Span
Last Post by firstPerson
0

int *ptr; means "create an int-pointer named ptr", double *ptr;means ""create a double-pointer named ptr", so T *ptr; means "create a T-pointer named ptr." The type T will be whatever type you actually use when you use this template.

0

Do I still need to defined these(under the public in a class of Array)?

T& operator[](int index);
const T& operator[](int index) const;
0

Usually you overload the operator[] for constant-ness. Are those operators defined in some class?

Consider these statement

T* a = getA();    
const T*b = getB();

if(a[0] == 1) doA();
if(b[0] == 1) doB();

Without the const T& operator[](int)const version, the b[0] == 1 would not compile because b is declared as a constant, therefore could only call member functions that are constant.

Edited by firstPerson

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.