why can't we declare object of a class inside the same class?

后端 未结 6 689
礼貌的吻别
礼貌的吻别 2020-12-01 19:10
class A
{
  A a;//why can\'t we do this
};
6条回答
  •  粉色の甜心
    2020-12-01 19:33

    In C++ : You can not do this, As it will be recursive structure (no end for calculating object size) , to Overcome this problem,
    Use Self Referential Pointer i.e. the Pointer having the address of Same class type.

    class A
    {
        A* aObj; // Self Referential Pointer
    }
    

提交回复
热议问题