Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize the reference member variable of a class?

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?

like image 467
banarun Avatar asked Mar 14 '13 07:03

banarun


People also ask

How do you initialize a reference variable?

There are three steps to initializing a reference variable from scratch: declaring the reference variable; using the new operator to build an object and create a reference to the object; and. storing the reference in the variable.

How do you initialize a class variable?

To initialize a class member variable, put the initialization code in a static initialization block, as the following section shows. To initialize an instance member variable, put the initialization code in a constructor.

How do you initialize a constant and reference member variable?

To initialize the const value using constructor, we have to use the initialize list. This initializer list is used to initialize the data member of a class. The list of members, that will be initialized, will be present after the constructor after colon. members will be separated using comma.


2 Answers

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.

like image 196
Daniel Frey Avatar answered Sep 24 '22 09:09

Daniel Frey


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.

like image 22
nvoigt Avatar answered Sep 25 '22 09:09

nvoigt