S
silverburgh.meryl
Hi,
I would like to create a Class which has a list of another class as it
attribute.
I wonder, should I do this:
class B {
private:
vector<A> _listOfA;
}
B::B() {
A a1;
A a2;
_listOfA.push_back(a1);
_listOfA.push_back(a2);
}
or this:
class B {
private:
vector<A*> _listOfA;
}
B::B() {
A* a1 = new A();
A* a2 = new A();
_listOfA.push_back(a1);
_listOfA.push_back(a2);
}
I think #1 is the preferred way in C++. But isn't there is a
performance penalty in #1?
(when you push_back(a1), you are copying a1 to the list of A, right? So
should we do #2, which is pass by reference instead of pass by value?
I would like to create a Class which has a list of another class as it
attribute.
I wonder, should I do this:
class B {
private:
vector<A> _listOfA;
}
B::B() {
A a1;
A a2;
_listOfA.push_back(a1);
_listOfA.push_back(a2);
}
or this:
class B {
private:
vector<A*> _listOfA;
}
B::B() {
A* a1 = new A();
A* a2 = new A();
_listOfA.push_back(a1);
_listOfA.push_back(a2);
}
I think #1 is the preferred way in C++. But isn't there is a
performance penalty in #1?
(when you push_back(a1), you are copying a1 to the list of A, right? So
should we do #2, which is pass by reference instead of pass by value?