开发者

Having an object contain another object of the same type?

I want to create an object that contains an object of the same type. When you create an object of this type, it creates another which cre开发者_如何转开发ates another and so on until the length is exhausted. However, I get a taking address of temporary warning. How do I get around this?

class A {
   A(int len) {
      if(len > 0) {
         _a = & A(len-1);
      }
      else {
         _a = NULL;
      }
   }

   A* _a;
}


Rather than taking the address of a temporary, instead create a new object:

class A {
   A(int len) {
      if(len > 0) {
         _a = new A(len-1);
      }
      else {
         _a = NULL;
      }
   }

   A* _a;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜