c++ - error lvalue when use singleton contain array of pointer -
my singleton like
class valuelstnode { private: zwnode* m_pzwnode[32]; public: valuelstnode(); valuelstnode(const valuelstnode& rhs); static valuelstnode& getinstance(); virtual ~valuelstnode(); valuelstnode& operator= (const valuelstnode& rhs); zwnode_p getnode (int posion) const; zwnode_p operator[] (int posion); const zwnode_p operator[] (int byposion) const; };
and file .cpp
valuelstnode::valuelstnode() { (u32_t = 0; < 32; i++) { m_pzwnode[i] = null; } } valuelstnode::valuelstnode( const valuelstnode& rhs ) { (u32_t = 0; < 32; i++) { m_pzwnode[i] = rhs.m_pzwnode[i]; } } valuelstnode& valuelstnode::operator= ( const valuelstnode& rhs ) { (int = 0; < zw_max_nodes; i++) { m_pzwnode[i] = rhs.m_pzwnode[i]; } return *this; } valuelstnode::~valuelstnode() { (int = 0; < zw_max_nodes; i++) { if (m_pzwnode[i] != null) { delete m_pzwnode[i]; m_pzwnode[i] = null; } } } valuelstnode& valuelstnode::getinstance() { static valuelstnode m_instance; return m_instance; } zwnode* valuelstnode::operator[] ( int posion ) { return m_pzwnode[posion]; } const zwnode* valuelstnode::operator[] ( int posion ) const { return m_pzwnode[posion]; }
but
valuelstnode m_valuelstnode = valuelstnode::getinstance(); m_valuelstnode[0] = null;
i error: lvalue required ... how solve this. me. thank & rg.
what have created isn't conforming idiomatic singleton pattern. lets see:
class valuelstnode { // ... public: valuelstnode(); // allow pulic construction of new instances valuelstnode(const valuelstnode& rhs); // allow new instances copies static valuelstnode& getinstance(); virtual ~valuelstnode(); valuelstnode& operator= (const valuelstnode& rhs); // well, may right intend, // not idiomatic // singleton zwnode_p getnode (int posion) const; // return copies of // internally managed elements? zwnode_p operator[] (int posion); // ^^^^ const zwnode_p operator[] (int byposion) const; // ^^^^ };
as want operate on singleton, need keep state within single class.
so need return class members manipulate references:
zwnode_p& operator[] (int posion); const zwnode_p& operator[] (int posion) const;
also singleton implementation should explicitly deny creating copies of class:
class valuelstnode { valuelstnode(const valuelstnode&) = delete; valuelstnode& operator=(const valuelstnode&) = delete; };
and take reference if need it:
valuelstnode& m_valuelstnode = valuelstnode::getinstance();
though, don't use case of singleton pattern applied naming. semantics feel broken.
i don't see how makes sense valuelstnode
should need singleton. did mean create factory pattern rather?
Comments
Post a Comment