c++ - Changing reference for one object and it changes for all in the collection -


i have created class person member of class account , there class bank contains vector of accounts.

i have created 1 person ownes 3 accounts wanted changeonwner() of 1 account accidentally accounts new owner. code running , rather intuitive. i not understand why reference in 3 accounts has changed. how fix that?

#include <iostream> #include <vector> using namespace std; class person{ public:     char* name;     int age;      person(char* name, int age){         this->name = name;         this->age = age;     }     ~person(){     }      void show(){         cout<<name<<" "<<age<<" yo";     } }; class account{ public:     person& owner;     double money;     account(person* owner, double money):         owner(*owner) , // this->owner = *owner;         money(money)  { //uninitialized reference member     }      void show(){         cout<<"\n-------------\n";         owner.show();         cout<<endl;         cout<<money<<" usd\n-------------";     } };  class bank{ public:     vector<account*>* v;     bank(){         v = new vector<account*>();     }     account* opennewaccount(person* client, double money){         account* r = new account(client, money);         v->push_back(r);         return r;     }     void zmienwlasciciela(person& o, account* r){         r->owner = o;     }     void usunrachunek(account* r){         delete r;     }     void show(){         for(int = 0; < v->size(); i++)             v->at(i)->show();     }     ~bank(){         delete v;     } };    int main(){     bank* bank = new bank();     person* thomas = new person("thomas", 34);     account* r1 =  bank->opennewaccount(thomas, 64363.32);     account* r2 =  bank->opennewaccount(thomas, 41251.54);     account* r3 =  bank->opennewaccount(thomas, 3232.32);     bank->show();      person* margaret = new person("margaret", 23);     bank->zmienwlasciciela(*margaret, r2);     cout<<"i have changed owner of account r2"<<endl;     bank->show();     return 0; } 

really nasty have reference member variable: avoid person& owner; have value.

the reason being thing constructing account object may go out of scope , you'll left dangling reference.

your person class has problem in if copied person;

person a("name", 20); person b = a; 

the name data both point same area of memory. not name change on 1 change name on other. around you'll have override copy constructor, or else this...

also name data used construct person may go out of scope , you'll access violations! in way i've used class above, i'd problems constant string "name" go out of scope after first ';'.

your best bet use std::string name see you're using stl.

putting together,

class person{ public:     std::string m_name;     unsigned m_age;      person(char* name, unsigned age) :         m_name(name),         m_age(age)     {     }     ~person()     {     }      void show(){         cout<<m_name.c_str()<<" "<<age<<" yo";     } }; 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -