Can someone explain me why it isn’t possible for the class TaxWay in the code to hold a member variable Bank that is initialized by a reference? What should I change in the code to make it correct?
When I change the member variable to a reference as Bank&, then it works. I thought that the same should happen with a “not reference variable”. How can it be done?
class Bank;
class TaxWay : public Way
{
public:
TaxSquare(int, int, Bank&);
virtual void process();
private:
int taxAmount;
Bank bank;
};
TaxWay::TaxWay(int anID, int amount, Bank& theBank)
: Way(anID),taxAmount(amount),bank(theBank)
{
}
I create an object as:
TaxWay TaxWay9(9,150, theBank);
In the example the
TaxWayclass cannot hold a copy of the bank, because you have not defined theBankclass. At a minimum the size of theBankclass must be known so space can be allocated.On the other hand, depending on what the
Bankcontains, it might not be a good idea to copy it. Will that also copy the money in the bank? 🙂