Reputation: 30615
I have a large number of objects to create and they will all be stored in one particular vector. So I did this:
std::vector<boost::shared_ptr<MyClass>> t;
t.resize(5000);
I then wrote a script to generate the C++ for creating all 5000 objects:
t[0] = boost::make_shared<MyClass>(4659);
t[1] = boost::make_shared<MyClass>(45029);
t[2] = boost::make_shared<MyClass>(2731);
.
.
t[4999]....
However, at the end I loop through and it appears I have not constructed the objects correctly:
for(boost::shared_ptr<MyClass> s : t){
//I cannot see the data members for s when debugging here
s->doSomething(x);
}
and it seems s
is not instantiated.
Have I mis-used boost::make_shared
and I am not actually creating objects?
Upvotes: 1
Views: 304
Reputation: 249333
You do not need to generate C++ code for this. Given your example, you should simply generate a text file containing the integers to construct the items with, then do this:
vector<shared_ptr<MyClass>> t;
for (int x; cin >> x; ) {
t.push_back(make_shared<MyClass>(x));
}
Of course you could use any istream instead of cin
.
If you do this, I promise you will not have any "missing" elements in your vector.
Upvotes: 2