Read the compiler message: operator [] MUST be non-static member of a class. No operator[] in X class definition.
Three operators must be non-static members in C++:
operator=
operator[]
operator->
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
Of course, you can't redefine pointers behaviour in C++ (thank God! ;) ). See what you want to do (and, fortunately, can do):
template <typename T>
class X {
public:
~X() { cout << "RIP X" << endl; }
class Y {
public:
void whoareyou() const {
cout << "It\'s me, Boss!" << endl;
}
};
Y* operator[](int i) { return y+i; } //!!!
private:
Y y[2];
};
int main()
{
X<int>& x = *new X<int>(); // a reference, not a pointer to!
X<int>::Y* py = x[0];
py->whoareyou();
delete &x;
Try this and see what happens.
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348