Similar questions can be found: Can a c++ class include itself as an member? and Create an instance of a class in the class itself
They are elucidative, but the "memory" factor that almost everybody says its not clear to me. I agree with the "loop structure" in the compilation, but I don't see the memory usage.
struct A {
A data_member;
};
int main(){ A object_1;}
In the code above data_member
is a subobject of object_1
. So data_member
is nested in object_1
(source). If the data_member
object were created in the same storage of object_1
than the lifetime of object_1
wouldn't end (source_2).
If we changed the A data_member
for int hi
, then sizeof(object_1)
would be 4 and the pointer to the object_1
would point to the memory of int hi
. Similarly it occurs the same when creating an char
, long long int
, bool
data member, so I assume it must be the same for a A
type.
My point is that when we are creating object_1
it creates infinitely objects, all of them in the exactly memory location. Where does come this use of memory ?
Edit 1: Since aparently all infinity objects would be created in the same memory location, somewhat "overriding".