constructor - Uninitialized reference member in C++ -


i have made class in c++ , wanted have field osoba& weird error:

class rachunek{ public:     osoba& wlasciciel;     double stan_konta;     rachunek(osoba* wlasciciel, double stan_konta){ //uninitialized reference member         this->wlasciciel = wlasciciel;         this->stan_konta = stan_konta;      } }; 

use initializing list this: (best approach)

class rachunek{ public:     osoba& wlasciciel;     double stan_konta;     rachunek(osoba* wlasciciel, double stan_konta):          wlasciciel(*wlasciciel) ,          stan_konta(stan_konta)  { //uninitialized reference member       } }; 

you have reference member , reference must initialized right away. notation allows initialization @ declaration time. if instead used normal member without & work fine did it. though presented style here more efficient.

alternativly: (lesser efficient approach)

class rachunek{ public:     osoba wlasciciel; // note missing & on type.      double stan_konta;     rachunek(osoba* wlasciciel, double stan_konta)     {         this->wlasciciel = *wlasciciel;           this->stan_konta = stan_konta;        } }; 

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 -