在构造函数 C++ 中初始化引用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6576109/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 20:22:59  来源:igfitidea点击:

Initialising reference in constructor C++

c++constructor

提问by ale

I don't think is a duplicate question. There are similar ones but they're not helping me solve my problem.

我不认为是重复的问题。有类似的,但他们没有帮助我解决我的问题。

According to this, the following is valid in C++:

根据这个,下面是在C ++中有效:

class c {
public:
   int& i;
};

However, when I do this, I get the following error:

但是,当我这样做时,我收到以下错误:

error: uninitialized reference member 'c::i'

How can I initialise successfully do i=0on construction?

如何i=0在构造上成功初始化?

Many thanks.

非常感谢。

回答by Kerrek SB

There is no such thing as an "empty reference". You haveto provide a reference at object initialization. Put it in the constructor's base initializer list:

没有“空引用”这样的东西。您必须在对象初始化时提供引用。把它放在构造函数的基本初始化列表中:

class c
{
public:
  c(int & a) : i(a) { }
  int & i;
};

An alternative would be i(*new int), but that'd be terrible.

另一种选择是i(*new int),但这会很糟糕。

Edit:To maybe answer your question, you probably just want ito be a member object, not a reference, so just say int i;, and write the constructor either as c() : i(0) {}or as c(int a = 0) : i(a) { }.

编辑:为了回答您的问题,您可能只想i成为成员对象,而不是引用,所以只需说int i;,并将构造函数编写为c() : i(0) {}或 as c(int a = 0) : i(a) { }

回答by Igor Oks

回答by fulmicoton

Apart from the sweet syntax, a key feature of references is that you are pretty sure that it always point to a value (No NULL value).

除了甜美的语法之外,引用的一个关键特性是您非常确定它始终指向一个值(无 NULL 值)。

When designing an API, it forces user to not send you NULL. When consuming an API, you know without reading the doc that NULL is not an option here.

在设计 API 时,它会强制用户不要向您发送 NULL。在使用 API 时,您无需阅读文档就知道 NULL 不是此处的选项。

回答by Constantinius

References have to be initialized upon creation. Thus you have to initialize it when the class is created. Also you have to provide some legal object to reference.

引用必须在创建时初始化。因此,您必须在创建类时对其进行初始化。此外,您必须提供一些合法的对象以供参考。

You have to use the initializer in your constructor:

您必须在构造函数中使用初始化程序:

class c {
public:
   c(const int& other) : i(other) {}
   int& i;
};

回答by G.S. Janjua

Reference should be initialised either by passing data to constructor or allocate memory from heap if you want to initialize in default constructor.

如果要在默认构造函数中初始化,则应通过将数据传递给构造函数或从堆分配内存来初始化引用。

class Test  
{  
    private:
        int& val;  
        std::set<int>& _set;  
    public:  
        Test() : val (*(new int())),  
                _set(*(new std::set<int>())) {  }  

        Test(int &a, std::set<int>& sett) :  val(a), _set(sett) {  }  
};  

int main()  
{  
    cout << "Hello World!" << endl;  
    Test obj;  
    int a; std::set<int> sett;  
    Test obj1(a, sett);  
    return 0;  
}


Thanks

谢谢

回答by Tobin K Tomy Karukulathel

template<class T>
class AVLNode {

private:
       T & data;
public:
       AVLNode(T & newData) {
            data = newData;
        }
};