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?

Recommended Answers

All 3 Replies

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.

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;

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.