I have class "Obraz", inside her exist vector which need to collect object another class. My problem is when i add a "Trojkat" object it calls the constructor twice. From what I understand this is because vector adds copies of the argument. Is there any way how can I fix this? Because I only need one constructor call.
Fragment of Main:
Obraz o;
cin >> menu;
switch (menu){
case 1:
Trojkat t1;
o.dodajTrojkat(t1);
break;
Fragment class Obraz:
class Obraz {
private:
vector <Trojkat> listaT;
public:
void dodajTrojkat(Trojkat& t) {
listaT.push_back(t);
}
};
//EDIT: I have a problem with my copy constructor. Because when I add for example a second object. Without parmeter the constructor is called twice. And when I add the third object, the constructor is called three times.
Fragment Main:
cin >> menu;
switch (menu){
case 1:
o.dodajTrojkat();
break;
Fragment class Obraz:
class Obraz {
private:
vector <Trojkat> listaT;
public:
void dodajTrojkat() {
listaT.emplace_back();
}
};
And other class:
class Punkt {
private:
int x, y;
public:
Punkt(){
cout << "Write x:";
cin >> x;
cout << "Write y:";
cin >> y;
}
Punkt(int _x, int _y){
x = _x;
y = _y;
}
Punkt(const Punkt &p){
x = p.x;
y = p.y;
}
};
class Linia {
private:
Punkt p1, p2;
public:
Linia(Punkt &_p1, Punkt &_p2) {
p1 = _p1;
p2 = _p2;
}
Linia(const Linia &l) {
p1 = l.p1;
p2 = l.p2;
}
};
class Trojkat {
private:
Linia l1, l2, l3;
public:
Trojkat(Linia& _l1, Linia& _l2, Linia& _l3) {
l1 = _l1;
l2 = _l2;
l3 = _l3;
}
Trojkat(const Trojkat& t) {
l1 = t.l1;
l2 = t.l2;
l3 = t.l3;
}
};