C++same class as member in class

后端 未结 3 828
轮回少年
轮回少年 2020-12-21 13:53

I have a class that should have a class of the same type as its member.

My declaration is the following:

class clsNode
{
private:
     clsNode m_Moth         


        
3条回答
  •  半阙折子戏
    2020-12-21 14:41

    You can't have a member of the same type inside the class. The compiler tries to calculate the size of the object and sort of "gets into a loop." You can get around that by using indirection. For example, you can store the pointer to the mother node.

    class clsNode
    {
    private:
         clsNode* m_Mother;
    public:
         void setMother(const clsNode &uNode, int index);
    };
    

提交回复
热议问题