14

Consider the following code 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;
}

It is showing the following error while using gcc compiler

    est.cpp: In constructor ‘Test::Test(int&)’:
    est.cpp:8:5: error: uninitialized reference member ‘Test::t’ [-fpermissive]

Why doesn't the compiler directly call the Constructor?

1
  • 1
    While the answers explain how to solve this, I'd like to point out that the underlying issue is that initialisation actually occurs before the constructor's body is entered, to make sure all the members are in a valid state before being used. Since references must be initialised, and can't be "retargeted" after initialisation, they logically need to be pointed to an actual variable before the constructor's body is entered as well. Commented Jun 8, 2017 at 23:28

2 Answers 2

25

That is because references can only be initialized in the initializer list. Use

Test (int &x) : t(x) {}

To explain: The reference can only be set once, the place where this happens is the initializer list. After that is done, you can not set the reference, but only assign values to the referenced instance. Your code means, you tried to assign something to a referenced instance but the reference was never initialized, hence it's not referencing any instance of int and you get the error.

3
  • They can also be initialized via a brace-or-equal-initializer, although that would be an uncommon use case
    – M.M
    Commented Jun 8, 2017 at 23:15
  • How would you do this if you needed a default constructor for Test?
    – Tom Auger
    Commented Jan 30, 2022 at 21:05
  • @TomAuger Add a static dummy int in the class (or somewhere else) and bind the reference to that. Commented Jan 31, 2022 at 12:29
3

My compiler emits this error:

error C2758: 'Test::t' : must be initialized in constructor base/member initializer list

And that's exactly what you must do. References must be initialized in the initializer list:

#include<iostream>

using namespace std;

class Test {
    int &t;
public:
    Test (int &x) : t(x) {  } // <-- initializer list used, empty body now
    int getT() { return t; }
};

int main()
{
    int x = 20;
    Test t1(x);
    cout << t1.getT() << " ";
    x = 30;
    cout << t1.getT() << endl;
    return 0;
}

Explanation:

If the reference is not in the initiliazer list, it's next to impossible for the compiler to detect if the reference is initialized. References must be initialized. Imagine this scenario:

Test (int &x, bool b) 
{
   if( b ) t = x;
}

Now it would be up to the caller of the constructor to decide if valid code was generated. That cannot be. The compiler must make sure the reference is initialized at compile time.

1
  • 4
    Not to mention the fact that t = x is not reference initialisation. It's an assignment. For references, these are very different: initialisation binds the reference to an object, assignment assigns into the object to which the reference is bound. Commented Mar 14, 2013 at 8:03

Not the answer you're looking for? Browse other questions tagged or ask your own question.