Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reference as class member initialization

Tags:

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?

#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 
like image 597
arjacsoh Avatar asked Nov 27 '11 10:11

arjacsoh


People also ask

Can a reference be a class member?

There are a few important points to note when using references as class members: You need to ensure that the referred object is guaranteed to exist till your class object exists. You need to initialize the member in the constructor member initializer list.

Can a reference be initialized?

When you initialize a reference, you bind that reference to an object, which is not necessarily the object denoted by the initializer expression. Note that the initialization of a reference is not the same as an assignment to a reference.

How do you initialize a data member of a class?

Static Data Member Initialization in C++ We can put static members (Functions or Variables) in C++ classes. For the static variables, we have to initialize them after defining the class. To initialize we have to use the class name then scope resolution operator, then the variable name. Now we can assign some value.


2 Answers

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) {} 
like image 178
Oliver Charlesworth Avatar answered Sep 19 '22 12:09

Oliver Charlesworth


“'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) {} 
like image 20
Karl Knechtel Avatar answered Sep 17 '22 12:09

Karl Knechtel