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

后端 未结 6 704
礼貌的吻别
礼貌的吻别 2020-12-01 19:10
class A
{
  A a;//why can\'t we do this
};
6条回答
  •  天命终不由人
    2020-12-01 19:32

    I take it you're coming from Java or something? A a will create a full instance of type A, which, well, contains A, which contains A, which contains A.

    You're probably thinking about this:

    class A
    {
      A *a; // A pointer to A, not a full instance
    };
    

提交回复
热议问题