Overloading [] with container or pointers
I have a class:
1 2 3 4 5
|
class Foo
{
private:
MyType* things[10];
};
|
While I would like to overload the [] operator for the use as this:
1 2
|
Foo myFoo;
myFoo[0] = myFoo[1];
|
Right now I am getting ugly:
1 2 3 4 5
|
MyType** operator[](size_t idx) { return &(things[idx]);
//...
*(myFoo[0]) = *(myFoo[1]);
|
Anything to fix that up a little? Thanks
Last edited on
return a reference:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
class Foo
{
private:
MyType things[10];
public:
MyType& operator [] (size_t index) { return things[index]; }
};
//
Foo myFoo;
myFoo[0] = myFoo[1]; // same as myFoo.things[0] = myFoo.things[1]
|
Topic archived. No new replies allowed.