0

This may sound silly, I don't know.

class Example {
 private:
       Example x; 
 public:
       //Methods
 };

It is possible in JAVA, but in C++ it is showing error. (field 'x' has incomplete type )

How can I do this in C++? If not what are other ways to do the same. I know one, pointers :) Anything else?

Also, I would be happy to know, what restricts to do the same in C++.

Thanks.

Deepak
  • 123
  • 1
  • 1
  • 7

1 Answers1

1

Ref: Self Referential Structures

This is not allowed in c++ as it will be recursive structure. However, you can use a Pointer having the address of same class type(Self Referential Pointer)

class A
{
    A* aObj; // Self Referential Pointer
}
user4581301
  • 33,082
  • 7
  • 33
  • 54