I would like to have several types that share the same implementation but still are of different type in C++.
To illustrate my question with a simple example, I woul
Use templates, and use a trait per fruit, for example:
struct AppleTraits
{
// define apple specific traits (say, static methods, types etc)
static int colour = 0;
};
struct OrangeTraits
{
// define orange specific traits (say, static methods, types etc)
static int colour = 1;
};
// etc
Then have a single Fruit class which is typed on this trait eg.
template
struct Fruit
{
// All fruit methods...
// Here return the colour from the traits class..
int colour() const
{ return FruitTrait::colour; }
};
// Now use a few typedefs
typedef Fruit Apple;
typedef Fruit Orange;
May be slightly overkill! ;)