Home > Back-end >  How to add variable to derived initialization list from base class initialization list?
How to add variable to derived initialization list from base class initialization list?

Time:05-24

I have a base class ShowTicket with parameterized constructor:

    //constructor
        ShowTicket(const char* Row, const char* SeatNumber):
        sold_status{false},
        row(Row),
        seat_number(SeatNumber)
        {}

I am creating a derived class, SportTicket that will take the same parameters as ShowTicket but will add a new boolean value to keep track of beer_sold. The problem is I do not know how to tell C that I still want sold_status to be intialized to false in the SportTicket Constructor.

I tried doing this:

    //Constructor
    SportTicket(const char* Row, const char* SeatNumber):
    ShowTicket(Row, SeatNumber),
    beer_sold{false},
    sold_status{false}
    {}

But I received the following error message:

Member initializer 'sold_status' does not name a non-static data member or base class

Is sold_satus already initialized to false because the variable is inherited from the base classes initialization list or is there a different syntax I can use to bring this variable into my derived class?

CodePudding user response:

The constructor of the class ShowTicket itself initializes its data member sold_status to false

//constructor
    ShowTicket(const char* Row, const char* SeatNumber):
    sold_status{false},
    row(Row),
    seat_number(SeatNumber)
    {}

So in the derived class just remove the line

sold_status{false}

because it is incorrect and redundant.

//Constructor
SportTicket(const char* Row, const char* SeatNumber):
ShowTicket(Row, SeatNumber),
beer_sold{false}
{}
  • Related