Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

const reference must be initialized in constructor base/member initializer list

I am trying to block access to the default constructor of a class I am writing. The constructor I want others to use requires a const reference to another object. I have made the default constructor private to prevent others from using it. I am getting a compiler error for the default constructor because the const reference member variable is not initialized properly. What can I do to make this compile?

class CFoo
{
public:
    CFoo();
    ~CFoo();
};

class CBar
{
public:
    CBar(const CFoo& foo) : fooReference(foo)
    {
    }

    ~CBar();

private:
    const CFoo& fooReference;

    CBar() // I am getting a compiler error because I don't know what to do with fooReference here...
    {
    }
};
like image 896
Trevor Balcom Avatar asked Jul 02 '10 18:07

Trevor Balcom


People also ask

Can const be initialized in constructor?

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.

Why a reference or const data member of a class must be initialized in the member initialization list of the constructor?

Reason for initializing the const data member in the initializer list is because no memory is allocated separately for const data member, it is folded in the symbol table due to which we need to initialize it in the initializer list.

Why reference members must be initialized using initializer list?

And in in computation phase which typically starts with the '{' of constructor body, if you do an assignment then compiler will complain as the reference member was already intialized. So, your only chance to initialize such member is the constructor initializer list.

What must be initialized in a constructor?

A class object with a constructor must be explicitly initialized or have a default constructor. Except for aggregate initialization, explicit initialization using a constructor is the only way to initialize non-static constant and reference class members.


2 Answers

don`t declare default constructor. It is not available anyway (automatically that it's) if you declare your own constructor.

class CBar
{
public:
    CBar(const CFoo& foo) : fooReference(foo)
    {
    }
private:
    const CFoo& fooReference;
};

fairly comprehensive explanation of constructors can be found here: http://www.parashift.com/c++-faq-lite/ctors.html

like image 186
Anycorn Avatar answered Sep 23 '22 22:09

Anycorn


The easiest way to create the default constructor you don't wanna use (that is the case with your constructor, is that right?) is just not defining it, that is:

class CBar
{
public:
    CBar(const CFoo& foo) : fooReference(foo)
    {
    }

    ~CBar();

private:
    const CFoo& fooReference;

    CBar();
};

In this case, it may be a little superfluous, because the compiler will not create a default constructor for a class with a reference member, but it's better to put it there in case you delete the reference member.

like image 23
jpalecek Avatar answered Sep 24 '22 22:09

jpalecek