简体   繁体   English

初始化列表:基类的构造函数和成员函数

[英]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: 考虑dateappointment ,使用您发布的另一个问题 ,如下所示:

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.

相关问题 将构造函数库和成员初始化器列表与继承一起使用 - Using constructor base and member initializer list with inheritance 为什么不能在派生类的构造函数初始化列表中初始化基类的数据成员? - Why can't Initialize the data member of base class in the constructor initializer list of derived class? 从成员初始化器列表调用基本方法 - Calling base methods from member initializer list 从基类构造函数调用派生类的成员函数 - Calling a member function of a derived class from the base class constructor 派生 class 构造函数初始值设定项列表中的基本 class 默认构造函数 - Base class default constructor in derived class constructor initializer list const引用必须在构造函数base / member初始化列表中初始化 - const reference must be initialized in constructor base/member initializer list 对对象的引用必须在构造函数库/成员初始化列表中初始化 - Reference to object must be initialized in constructor base/member initializer list 从成员构造函数抛出异常(大括号初始化程序与初始化程序列表) - Throwing exception from member constructor (brace initializer vs initializer list) 构造函数初始化程序列表中的错误 C2436 成员 function 或嵌套 class - error C2436 member function or nested class in constructor initializer list 派生类:在初始化列表中使用基类成员 - Derived class: using Base class member in initializer list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM