I'm having trouble with getting 2 different objects of the same type to have pointers to different objects using a parameter pack. Here is Controller.h:
using expand_type = int[];
template<typename... Ts> class Controller {
public:
Controller(int i, string s, Ts... args): id(i), name(s) {
expand_type{ (add_component(&args), 0)... };
}
void add_component(Test2* a) {
device_.push_back(a);
}
void print() {
cout<<name<<":"<<endl;
cout<<"================="<<endl;
for (vector<Test2*>::size_type i = 0; i < device_.size(); ++i) {
cout<<device_[i]->print()<<endl;
}
cout<<"================="<<endl;
}
private:
vector<Test2*> device_;
int id;
string name;
};
and here is my Test2.h:
class Test2 {
public:
Test2(): x(0) {}
Test2(int a): x(a) {}
int print() const {
return x;
}
private:
int x;
};
My problem is when I make two seperate Controller objects, they share their Test2 objects in their respective vectors. Here is my main:
int main() {
Test2 b(69);
Test2 c(666);
Test2 d(943754);
Controller<Test2, Test2, Test2, Test2> x(2, string("Peter"), 70, b, c, d);
Controller<> y(2, string("Pietje"));
Controller<Test2, Test2, Test2, Test2> z(3, string("Jan"), 909, 808, 1, 2);
x.print();
y.print();
z.print();
cout<<"hello"<<endl;
return 0;
}
then the output is:
Peter:
=================
0
808
1
2
=================
Pietje:
=================
=================
Jan:
=================
0
808
1
2
=================
hello
I want the different objects of Controller to have different pointers to different Test2 objects, and I'v no clue how.
Also as a side problem my first pointer always becomes 0, unless it's the only object in the vector.
Any help is appreciated!
Aucun commentaire:
Enregistrer un commentaire