hello all
hello all

Reputation: 252

Creating a public property inside a class which is an instance of this class

I'm creating a class named Circle and needs a public property to access its 'parent' Circle instance. Thus I code it like this:

class Circle {
public:
...
    Circle parent;
...
}

But this gave me an error: Incomplete type is not allowed

What should I do?

Upvotes: 1

Views: 74

Answers (2)

flamingo
flamingo

Reputation: 506

You should define parent as a pointer to Circle:

Circle *parent;

Upvotes: 0

That cannot be done. Consider what the memory footprint of your type would be: a Circle contains a Circle, so it size cannot be smaller than the inner Circle, but that size is the same as the size of the outer Circle, reaching a contradiction.

Maybe you meant to store a pointer or smart pointer? That is allowed, since the size of the pointer is known by the compiler.

Upvotes: 4

Related Questions