c++ - How I can auto increment each class objects? -
i have class contain 2 private int , 1 const (m_id) , other 1 static (next_id).
i want set m_id next_id , increment next_id each time create object of class.
but since it's const can't set :
class::class() { m_id = next_id++; }
i need set
class::class() :m_id(next_id) { next_id++; }
but that's not either because can't access private static that.
someone told me const not intented used that, remove it. solution?
edit : here full header , source
header
#ifndef entity_h_lea12oed #define entity_h_lea12oed #include "entitykey.h" #include "componentmanager.h" class entity { public: entity (); virtual ~entity (); private: ekey m_key; componentmanager m_componentmanager; const int m_id; static int next_id; }; #endif /* end of include guard: entity_h_lea12oed */
source
#include "entity.h" entity::entity() :m_id(next_id++) { } entity::~entity() { }
(of course entitykey , componentmanager doesn't have question)
(edit 2: corrected errors in code due testing)
you need define next_id
, or else compile, but not link. so:
class class { /* whatever */ }; class::class() :m_id(next_id++) { /* whatever */ } int class::next_id = 0;
Comments
Post a Comment