vendredi 30 juin 2017

Why don't you have to implement the constructor of class with a list of objects as data member?

Basically imaging that I have a class A and a class B.

Class A has several data members and class B has a list of A as a data member.

So basically I have my class A constructor by default and there I initialize all the data members like:

A::A(){
this->a = int();
this->s = string();
}

And a private data member of the class B is:

std::list<A> Alist;

One guy at work told me that I don't need to initiate the Alist variable because the compiler will do it automatically.

Normally I do the following:

B::B(){
this->Alist = std::list<A>();
}

Because supposing that for example I have a A object as data member of the class B. In order to create B variable I should make the constructor like:

this->avar = A();

So, Why shouldn't I code the constructor if the data member is a variable? I can't see an explanation.

Thanks.

Aucun commentaire:

Enregistrer un commentaire