¿Cómo inicializar la variable miembro de referencia de una clase?
Considere el siguiente código C ++:
#include<iostream>
using namespace std;
class Test {
int &t;
public:
Test (int &x) { t = x; }
int getT() { return t; }
};
int main()
{
int x = 20;
Test t1(x);
cout << t1.getT() << " ";
x = 30;
cout << t1.getT() << endl;
return 0;
}
Está mostrando el siguiente error al usar el compilador gcc
est.cpp: In constructor ‘Test::Test(int&)’:
est.cpp:8:5: error: uninitialized reference member ‘Test::t’ [-fpermissive]
¿Por qué el compilador no llama directamente al Constructor?