I have a class that holds an array of integers and to get a reference of this array the subscript operator [] is overloaded (this is a stripped down example with logic checks etc removed):
class Foo {
public:
Foo() {};
// and overload the [] operator
int& operator[](const int index);
private:
const int LEN = 7;
int arr[LEN];
};
int& Foo::operator[](const int index) {
return arr[index];
}
A pointer of an instance of such class (named boo) is passed to a function. Now what I want to do is:
int val = boo[0];
but it fails "error: invalid cast from type ‘Foo’ to type ‘int’".
My first idea was that Im passing a pointer to a class and I should bring a copy of the instance into scope and use that copy instead. This works. But Im curious if it would be possible to use the user defined type as a true built in type? Should I use a wrapper?