user5110759
user5110759

Reputation:

Invoking operator[] method from within object

For an implementation

template<class T, class C> 
class{
public:
...
T& operator(int)[];
private:
...
void do_something();

};

a definition such as

template<class T, class C> void Obj<T,C>::do_something(){
 auto some_count = 0;
 ...
 T& tmp = this->[some_count];

 ...
}

gets the following compilation error:

error: expected unqualified-id

Any idea what could be wrong?

TIA

Vinod

Upvotes: 1

Views: 20

Answers (1)

Daniel H
Daniel H

Reputation: 7443

The type of this is Obj<T, C>*. To use it like an instance of Obj<T, C>, just use *this.

For example,

T& tmp = (*this)[some_count];

Alternatively, you can use operator[] as any other member function:

T& tmp = operator[](some_count);

But that’s often more confusing, so you should prefer the first format.

Upvotes: 3

Related Questions