Trung Trinh Trung Trinh - 23 days ago 8
C++ Question

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() {
for (u32_t i = 0; i < 32; i++) {
m_pZwNode[i] = NULL;
}
}

ValueLstNode::ValueLstNode(
const ValueLstNode& rhs
) {
for (u32_t i = 0; i < 32; i++) {
m_pZwNode[i] = rhs.m_pZwNode[i];
}
}

ValueLstNode&
ValueLstNode::operator= (
const ValueLstNode& rhs
) {
for (int i = 0; i < ZW_MAX_NODES; i++) {
m_pZwNode[i] = rhs.m_pZwNode[i];
}
return *this;
}

ValueLstNode::~ValueLstNode() {
for (int i = 0; i < 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 get error: lvalue required ...
How to solve this. Help me.
Thank & rg.

Answer

What you have created isn't actually conforming the idiomatic Singleton Pattern. Lets see:

class ValueLstNode {
   // ...
public:
    ValueLstNode(); // You allow pulic construction of new instances
    ValueLstNode(const ValueLstNode& rhs); // You allow new instances as copies 
    static ValueLstNode& GetInstance();
    virtual ~ValueLstNode();

    ValueLstNode& operator= (const ValueLstNode& rhs); // Well, may be the right intend,
                                                       // but not really idiomatic for a 
                                                       // singleton

    ZwNode_p GetNode    (int Posion) const;            // You return copies of your internally
                                                       // managed elements?
    ZwNode_p operator[] (int Posion);                  // ^^^^ That
    const ZwNode_p operator[] (int byPosion) const;    // ^^^^ That
};

As you want to operate on a Singleton, you need to keep state within that single class.

So you need to return class members to manipulate as references:

ZwNode_p& operator[] (int Posion);
const ZwNode_p& operator[] (int Posion) const;

Also your Singleton implementation should explicitly deny creating copies of that class:

class ValueLstNode {
    ValueLstNode(const ValueLstNode&) = delete;
    ValueLstNode& operator=(const ValueLstNode&) = delete;
};

and take a reference if you need it:

ValueLstNode& m_ValueLstNode = ValueLstNode::GetInstance();

Though, I don't fully get the use case of a Singleton Pattern applied from your naming. The semantics feel broken.

I don't see how it makes sense ValueLstNode should need to be a Singleton. Did you mean to create a Factory Pattern rather?

Comments