[英]initializer list: a constructor from the base class and a member function
So what I want to do is 所以我想做的是
initialize my subclass's constructor with my base class's constructor. 用基类的构造函数初始化子类的构造函数。
this is what my base class constructor looks like. 这就是我的基类构造函数的样子。
Appointment::Appointment(string description, int month, int day, int year, int hour, int minute):date(month, day, year){
this-> hour = hour;
this-> minute =minute;
this-> description = description;
}
this is what my subclass constructor looks like 这是我的子类构造函数的样子
Daily::Daily(string description, int month, int day, int year, int hour, int minute) : Appointment(description, month, day, year, hour, minute){
}
^in my subclass's constructor (daily) there is an error that states that I need to explicitly initialize the member 'date', which does not have a default constructor. ^在我的子类的构造函数中(每天),出现一个错误,指出我需要显式初始化成员'date',该成员没有默认的构造函数。
How do I explicitly initialize both 'date' and 'Appointment' in my subclass constructor's initializer list? 如何在子类构造函数的初始值设定项列表中显式地初始化“日期”和“约会”?
Does it look something like this? 它看起来像这样吗?
Daily::Daily(string description, int month, int day, int year, int hour, int minute) : Appointment(description, month, day, year, hour, minute):Date(month, day, year)
Thanks 谢谢
Considering date
and appointment
, using this other question you posted, to be as: 考虑date
和appointment
,使用您发布的另一个问题 ,如下所示:
Date date;
Appointment appointment;
You can use this constructor syntax: 您可以使用以下构造函数语法:
Daily::Daily(string description, int month, int day, int year, int hour, int minute) : appointment(description, month, day, year, hour, minute), date(month, day, year) { ... }
Usually in a definition like: 通常在这样的定义中:
A::A(...) : x(...), y(...), ... {...}
x(...), y(...), ...
is an initializer-list which purpose is to initialize member objects of the class. x(...), y(...), ...
是一个初始化列表 ,其目的是初始化该类的成员对象。
应该用逗号隔开
Daily::Daily(string description, int month, int day, int year, int hour, int minute) : Appointment(description, month, day, year, hour, minute), Date(month, day, year)
if Daily class has a (Date) and has an (Appointment) which meanse it's a composition then you have to use the initializer list as shown below 如果Daily班级有一个(Date)和一个(Appointment)这意味着它是一个合成,那么您必须使用初始化器列表,如下所示
Daily::Daily(string description, int month, int day, int year, int hour, int minute) :
Appointment(description, month, day, year, hour, minute),
Date(month, day, year) {}
but if Daily has an (Appointment) only -composition- and as i can see the Appointment has a (Date) -composition- then the Daily constructor should be like this 但是如果Daily仅有一个(Apointpointment)-composition-,并且如我所见,该约会具有(Date)-composition-,则Daily构造函数应该像这样
Daily::Daily(string description, int month, int day, int year, int hour, int minute) :
Appointment(description, month, day, year, hour, minute) {}
and the Appoiintment parameterized constructor will call the Date parameterized constructor 而Appoiintment参数化的构造函数将调用Date参数化的构造函数
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.