c++ - Retrieving and editing private members of objects in a vector -


i have code adds few objects vector. wish retrieve specific object vector , able both write out , edit private member variables.

this code have:

class product { public:     product(int n, const string& na, int p)             : number(n), name(na), price(p) {};     void info() const; private:     string name;     int number, price; }; 

the member function looks this:

void product::info() const {     cout << number << ". " << name << " " price << endl; } 

i create vector , push objects, so:

vector<product> range; range.push_back(product(1, "bagpipe", 25)); 

to retrieve , list information objects, have following function:

void listproducts (const vector<product>& range1) {     for_each (range1.begin(), range1.end(), mem_fun_ref(&product::info)); } 

but stuck.

to boil problem down: have no idea how retrieve individual objects vector , edit them. need able search vector objects containing specific number or name, , able retrieve either information private members, , able edit members.

ideas have solutions thusfar are:

  • to create additional member functions can return individual members

  • to create functions that, similar function have above, can search through each object in vector , use return these additional member functions compare i'm looking for

  • i don't quite know how go editing objects private members, current guess need members functions well, , functions tie in those

any appreciated! vague nudges in right direction!

if member variables private, definition, cannot access them outside world! need 1 of following:

  • change them public.
  • add accessor functions class (i.e. int myclass::getfoo() const , void myclass::setfoo(int)).
  • make function friend of class.

this nothing being stored in vector.


Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -