Reference as class member initialization

后端 未结 4 1291
野的像风
野的像风 2020-12-05 10:28

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:

相关标签:
4条回答
  • 2020-12-05 11:05

    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).

    0 讨论(0)
  • 2020-12-05 11:12

    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

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

    0 讨论(0)
  • 2020-12-05 11:13

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

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

    "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 bank member, 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.

    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 should still initialize that with the initialization list, even if you could get away without. Same goes for the taxAmount, really.

    // 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) {}
    
    0 讨论(0)
  • 2020-12-05 11:15

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

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

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

    TaxSquare::TaxSquare(int anID, int amount, Bank& theBank)
    : Square(anID), taxAmount(amount), bank(theBank)
    {}
    
    0 讨论(0)
提交回复
热议问题