Why a nested class can't have a member the type of which is the one of the enclosing class?

Viewed 215

One of the methods of a class C needs to return a struct containing a pair of integers and a fresh instance of C. It may look awkward, but given the overall design, this makes a lot of sense (think of a Waveform class returning a range of itself as a copy, with the indication of where the range starts and ends).

The problem is that this doesn't seem to be allowed. I can redesign my class in order to circumvent this issue but can you explain me why, from the point of view of the compiler, this cannot be done

struct S {
    struct S2 {
        S s;
    };
};

as S is an incomplete type (this is the compiler error), and instead this is perfectly fine

struct C {
    struct C1 {
        C makeC() { return C(); }
    };
};

Where is a substantial difference?

5 Answers
Related