C++ 作为类成员初始化的引用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8285040/
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 18:19:05  来源:igfitidea点击:

Reference as class member initialization

c++constructorreference

提问by arjacsoh

I want to initialize a property of a class that holds a reference to another class by passing such a reference as a parameter to the constructor. However I receive an error:

我想通过将这样的引用作为参数传递给构造函数来初始化一个类的属性,该类保存对另一个类的引用。但是我收到一个错误:

“'TaxSquare::bank' must be initialized in constructor base/member initializer list”. What is wrong in the following code of the classes?

“'TaxSquare::bank' 必须在构造函数基/成员初始化列表中初始化”。以下类的代码有什么问题?

#ifndef TAXSQUARE_H
#define TAXSQUARE_H
#include "Square.h"

class Bank;

class TaxSquare : public Square
{
    public:
      TaxSquare(int, int, Bank&);
      virtual void process();

    private:
      int taxAmount;
      Bank& bank;

};
#endif
#include <iostream>
#include "TaxSquare.h"
#include "Player.h"
#include "Bank.h"
using namespace std;

TaxSquare::TaxSquare(int anID, int amount, Bank& theBank) : Square(anID)
{
  taxAmount = amount;
  bank = theBank;
}
#ifndef BANK_H
#define BANK_H

class Bank
{
public:
  Bank(int, int, int);
  void getMoney(int);
  void giveMoney(int);
  void grantHouse();
  void grantHotel();

private:
  int sumMoney;
  int numOfHouses;
  int numOfHotels;

};

#endif

回答by Oliver Charlesworth

You are attempting to assign to bank, not initialize it:

您正在尝试分配给bank,而不是初始化它:

TaxSquare::TaxSquare(int anID, int amount, Bank& theBank) : Square(anID)
{
    // These are assignments
    taxAmount = amount;
    bank = theBank;
}

bankis a reference, and therefore it must be initialized. You do so by putting it in the initializer list:

bank是一个引用,因此它必须被初始化。你可以把它放在初始化列表中:

TaxSquare::TaxSquare(int anID, int amount, Bank& theBank)
: Square(anID), taxAmount(amount), bank(theBank)
{}

回答by Karl Knechtel

“'TaxSquare::bank' must be initialized in constructor base/member initializer list”. What is wrong in the following code of the classes?

“'TaxSquare::bank' 必须在构造函数基/成员初始化列表中初始化”。以下类的代码有什么问题?

What is wrong is that TaxSquare::bankis not being initialized in the constructor base/member initialization list, exactly as it says.

问题在于它TaxSquare::bank没有在构造函数基/成员初始化列表中初始化,正如它所说的那样。

"The constructor base/member initialization list" is the initialization list for the constructor in question, TaxSquare::TaxSquare(int, int, Bank&). You're already using it to initialize the base (Square). You must use it to initialize the bankmember, because it is of a reference type. Things not specified in the initialization list get default-initialized, and there is no default-initialization for references, because they must always reference something, and there is no default something for them to reference.

“构造函数基/成员初始化列表”是有问题的构造函数的初始化列表TaxSquare::TaxSquare(int, int, Bank&)。您已经在使用它来初始化基础 ( Square)。您必须使用它来初始化bank成员,因为它是引用类型。未在初始化列表中指定的事物被默认初始化,并且引用没有默认初始化,因为它们必须始终引用某些东西,并且没有默认的东西可供它们引用。

Honestly, I find that using references for data members in C++ is more trouble than it's worth, 99% of the time. You're probably better off with a smart pointer, or even a raw one. But you shouldstill initialize that with the initialization list, even if you could get away without. Same goes for the taxAmount, really.

老实说,在 99% 的情况下,我发现在 C++ 中使用数据成员的引用比它的价值更麻烦。最好使用智能指针,甚至是原始指针。但是您仍然应该使用初始化列表对其进行初始化,即使您可以逃脱。这同样适用于taxAmount,真的。

// TaxSquare::TaxSquare(int anID, int amount, Bank& theBank) : Square(anID)
// That thing after the colon is the initialization list:      ^^^^^^^^^^^^
// So add the other members to it, and then notice that there is nothing left
// for the constructor body to do:
TaxSquare::TaxSquare(int anID, int amount, Bank& theBank) : 
Square(anID), taxAmount(amount), bank(theBank) {}

回答by vbar

The error is you're trying to assign through an uninitialized reference: a C++ reference cannot be assigned - the object it refers to is assigned instead - and so, if it's a member, it must be initialized in the initializer list (like the compiler says).

错误是您试图通过未初始化的引用进行分配:无法分配 C++ 引用 - 它引用的对象已被分配 - 因此,如果它是成员,则必须在初始化程序列表中对其进行初始化(如编译器说)。

回答by Riz

bank = theBank; this statement means you are assigning obj1 to obj2 and it will call Assignment operator which is wrong as bank is of type reference it must be intialized as mentioned below

银行 = 银行;此语句意味着您将 obj1 分配给 obj2 并且它会调用赋值运算符这是错误的,因为银行是类型引用它必须如下所述初始化

TaxSquare::TaxSquare(int anID, int amount, Bank& theBank) : Square(anID), bank(theBank) {}

TaxSquare::TaxSquare(int anID, int amount, Bank& theBank) : Square(anID), bank(theBank) {}