Software_t
Software_t

Reputation: 586

Define class with fields from the type of the class

Can someone explain me why I can't define something like that:

Class A {  
A a;
//...
};

But I can define something like that:

Class A {  
std::vector<A> vec;
//...
};

What is the difference that allow the second?

Upvotes: 2

Views: 89

Answers (1)

Zadowolony Pesymista
Zadowolony Pesymista

Reputation: 73

You can't use the first because it is recursive, that is object A contains object A, and the second one you can use because vector doesn't contain object A but a pointer to object A.

Upvotes: 1

Related Questions