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
Post a Comment