mercredi 25 mars 2015

Why we need to use initializer list while initializing reference members?

There are lot of questions on S.O related to initializer list ,read quite a few of them ,while reading I understood why we should use intializer list in intializing const member functions,but some how i am not able to understand on why we need to use initializer list for intializing the refernce members. Consider this sample code :



class Test {
int &t;
public:
Test(int &t):t(t) {}
int getT() { return t; }};
int main() {
int x = 20;
Test t1(x);
cout<<t1.getT()<<endl;
x = 30;
cout<<t1.getT()<<endl;
return 0;
}


can anyone please explain why we need to use this


Aucun commentaire:

Enregistrer un commentaire