I want to have a simple class that represents an object with unique id. Every new object gets an ID higher than the previously created one. Important thing to notice is that id for every object is constant, so I'm obliged to use initialise list. For some reason I get an error about undefined reference: Undefined reference to Test::ID.
class Test
{
const int m_id;
static int ID;
public:
Test() : m_id(ID++)
{
cout << "Created object with id: " << m_id << endl;
}
};
- Why is this?
- How can I fix this?
- How can I make sure that ID is pre-initialised with 0, so i don't increment an uninitialised variable?