Reputation: 2611
Consider I have some simple class:
template<typename T>
class Vector {
public:
T first() const { return this->m_first; }
T second() const { return this->m_second; }
private:
T m_first;
T m_second;
}
Now I want to create some meaning behind some of that:
typedef Vector<int> Position;
typedef Vector<unsigned int> Dimension;
Can I also create an alias to the methods of the new types Position and Dimension so that for Position, x() references frist(), y() references second() while for Dimension width() references first(), height() references second().
Or even more "speedy": Having first and second public, create aliases to the member variables? Real use case is more complex, but this should display what I want: Two things that are very similar in memory, but called completly different depending on the context.
Upvotes: 2
Views: 205
Reputation: 60430
You can achieve what you want by doing something like this:
class Position : private Vector<int> {
public:
auto x() const { return first(); }
auto y() const { return second(); }
};
class Dimension : private Vector<unsigned int> {
public:
auto width() const { return first(); }
auto height() const { return second(); }
};
which creates new types that inherit from a specific instantiation of Vector
, and call the privately inherited member functions with publicly accessible meaningful names.
Here's a demo.
Upvotes: 2