Solution 1:
For the static variable problem: declare your static variables in the class declaration:
class Enum
{
public:
static const Enum var1;
static const Enum var2;
Enum(int v):v(v)
{
}
private:
int v;
//operator == using v
};
Then, create a source file for this class, Enum.cpp
, containing:
#include "Enum.h"
const Enum Enum::var1 = Enum(1);
const Enum Enum::var2 = Enum(2);
Solution 2:
If you want it to be header-only, you can use static variables instead of class variables:
class Enum
{
public:
Enum(int v):v(v)
{
}
private:
int v;
};
namespace Enumeration // It is not possible to name it 'Enum'
{
// static => local to translation unit. No linking conflict
static const Enum var1 = Enum(1);
static const Enum var2 = Enum(2);
}
You can see a live example here. The only drawback is that you cannot use the name of the class for the namespace.