My job is to change y to 5 in ob1 and x to 10 in ob2, but it must be through container "myobjects". So for ex. program must find in Container "myobjects" ob2 access y and chages its value to 5.
class Data {
public:
Data(int x, int y) : x(x), y(y) {}
int x;
int y;
};
class Container {
private:
vector<Data> v1;
public:
void add(Data d) {
v1.push_back(d);
}
Data& get(size_t index) {
return v1[index];
}
};
// First object is index 0, second object is index 1
myobjects.get(0).x = 42; //or whatever.
myobjects.get(1).y = 3;
Note: I suggest only having ob1 and ob2 be temporaries in your code, because their only purpose is to be added to the collection.