I have a simple class that is the concatenation of 2 objects from other libraries:
class MyClass {
public:
Thing1 thing1;
Thing2 thing2;
};
What I want is to be able to ensure that when I plow through a list of thing1s and I want to know about something in the associated thing2, I can get the right answer. So I make a vector out MyClass:
std::vector<MyClass> myClasses;
So far so good. Now I want to populate this vector's thing1
members from a vector of thing1
s. All I could come up with is:
MyClass someStuff;
for(unsigned i=0; i<thing1Vec.size(); i++) {
someStuff.thing1 = thing1Vec[i];
myClasses.push_back(someStuff);
}
This works, but it seems inelegant and non-C++ to me.
I thought about inheriting from Thing1 and Thing2, but I have to pass instances to other methods later on. I don't know how else to set up the class that allows for this.
I'm a bit of a C++ newb, so I could be thinking about this problem all wrong. If anyone has better code fo this kind of thing, I'd love to see it.
I think this is exactly why people use Python.