Can a class be within itself (Self containing)?

Yes, but it has to be some form of pointer or reference.

A class can’t contain an instance of itself directly because until the compiler has finished processing the class, it doesn’t know how big that type will be. However, since a pointer (or reference) is always the same size, it can handle that just fine.

// This will work
struct Wooble
{
	Wooble *w;
};

// This will not
struct Wooble
{
	Wooble w;
};

Is it possible to have a class that contains a variable of itself. I’ve tried this before but the compiler fails to compile the class?

Any help would be great thanks.

very nicely detailed answer Jamie!

:slight_smile:

Rama