There are some problems associated with inheriting from standard classes; but if you agree to accept these problems, you can do something similar to what others have suggested, but use it instead std::valarray. It would look like this:
#include <valarray>
class rect : public std::valarray<int>
{
public:
rect() : std::valarray(4, 0) {}
int& x() { return (*this)[0]; }
int& y() { return (*this)[1]; }
int& width() { return (*this)[2]; }
int& height() { return (*this)[3]; }
};
In doing so, you inherit the vector operations defined on std::valarray<int>.
I would not recommend this approach. I just suggest that this can be done.
source
share