如何从基类初始化列表中添加变量到派生的初始化列表?
我有一个带有参数化构造函数的基类展示:
//constructor
ShowTicket(const char* Row, const char* SeatNumber):
sold_status{false},
row(Row),
seat_number(SeatNumber)
{}
我正在创建一个派生的类SportTicket,它将采用与ShowTicket相同的参数,但会增加一个新的布尔值,以跟踪Beer_sold。问题是我不知道如何告诉C ++我仍然希望Sold_status在SportTicket构造函数中被Intialial Intialial to false。
我尝试这样做:
//Constructor
SportTicket(const char* Row, const char* SeatNumber):
ShowTicket(Row, SeatNumber),
beer_sold{false},
sold_status{false}
{}
但是我收到以下错误消息:
成员初始化器“已sold_status'不命名非静态数据成员或base类
已出售为false,因为该变量是从基类的初始化列表中继承的,或者在那里。我可以用来将此变量带入我的派生类中的不同语法?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
类
showticket
本身的构造函数本身将其数据成员sold_status
tofalse
,因此在派生类中只需删除该行,
因为它不正确且冗余。
The constructor of the class
ShowTicket
itself initializes its data membersold_status
tofalse
So in the derived class just remove the line
because it is incorrect and redundant.