繁体   English   中英

通过初始化列表进行参数构造

[英]Parametric Construction Via Initializer List

我得到了以下代码来开发自己的约会簿应用程序:

#include<iostream> 
#include<string>
#include<vector>
#include "Appointment.h"
#include "OneTime.h"
#include "Daily.h"
#include "Monthly.h"
#include "Yearly.h"
using namespace std;

void checkAppointments(vector<Appointment*>& apptbook){
   // STATEMENTS
}
void addAppointment(vector<Appointment*>& apptbook){
  // STATEMENTS
}


int main(){

  vector<Appointment*> apptbook;

  char option;

  do {
     cout << "********** Appointment Book Application ************" << endl<< endl;
     cout << "(a) See all appointments on a given day." << endl;
     cout << "(b) Add an appointment." << endl << endl;
     cout << "Enter an option or 'q' to quit: ";

     cin >> option;
     switch(option){
        case 'a': 
            checkAppointments(apptbook);
            break;
        case 'b':
            addAppointment(apptbook);
             break;
        case 'q':
             break;
        default:
            cout << "You entered an invalid option.  Try again!";    
     }
     cout << endl;
  }
  while(option != 'q');

  // Cleaning up 

  for(int i = 0; i < apptbook.size(); i++){
    delete apptbook[i];
  }
  apptbook.clear();

  system("PAUSE");
  return 0;
}

我被要求使用下面指定的Appointment构造函数的一些参数来构造Appointment.h文件中包含的Appointment类的date成员:

Appointment(string description, int month, int day, int yr, int hr, int min)

defining the constructor in the requested way: 以下是我在迄今所做Appointment.h 定义所请求的方式构造:

#ifndef APPOINTMENT_H
#define APPOINTMENT_H

#include<string>
#include<sstream>
#include "Date.h"
using namespace std;

class Appointment{

  public:
    Appointment(string description, int month, int day, int yr, int hr, int min);
    Date date;

  private:
    int hour;
    int minute;
    string convertInt( int number ) const;
};
Appointment(string description, int month, int day, int yr, int hr, int min) : public date(month, day, yr)
{
    this->description = description;
}
string Appointment::convertInt( int number ) const
{
    stringstream ss;
    ss << number;
    return ss.str();
}

#endif

我想我的问题是这样的:“如何使用Appointment的某些参数来构造其初始值列表的日期成员?” 这个概念对我来说是陌生的,我遇到了麻烦。 这是Date.hDate.cpp

日期.h

#ifndef DATE_H
#define DATE_H

#include<string>
using namespace std;

class Date{

  public:
    Date(int month, int day, int year);

    int getMonth() const;
    int getDay() const; 
    int getYear() const;

  private:
    int month;
    int day;
    int year;
};

#endif

Date.cpp

#include "Date.h"
#include<string>
using namespace std;

Date::Date(int month, int day, int year) {
    this->month = month; 
    this->day = day;
    this->year = year;
}

int Date::getMonth() const{
  return month;
}

int Date::getDay() const{
  return day;
}

int Date::getYear() const{
  return year;
}

只需从约会构造器中删除public关键字,为什么将函数定义放在头文件中?

#ifndef APPOINTMENT_H
#define APPOINTMENT_H

#include<string>
#include<sstream>
#include "Date.h"
using namespace std;

class Appointment{

  public:
    Appointment(string description, int month, int day, int yr, int hr, int min);
    Date date;

  private:
    int hour;
    int minute;
    string convertInt( int number ) const;
};
Appointment(string description, int month, int day, int yr, int hr, int min) : date(month, day, yr)
{
    this->description = description;
}
string Appointment::convertInt( int number ) const
{
    stringstream ss;
    ss << number;
    return ss.str();
}

#endif

构造函数定义错误:

Appointment(string description, int month, int day, int yr, int hr, int min) : public date(month, day, yr)
{
    this->description = description;
}

您应该添加Appointement::作为前缀,并在其中删除public关键字:

Appointment::Appointment(string description, int month, int day, int yr, int hr, int min) : date(month, day, yr)
{
    this->description = description;
}

另外,请学习使用std::前缀,而不是using namespace std;盲目添加using namespace std; 那不是命名空间的用途。

在以下情况下对向量使用动态分配时,会出现另一个潜在的问题:

vector<Appointment*> apptbook;

没有必要。 您可以使用:

std::vector<Appointment> apptbook;

并像这样在addAppointment填充它:

void addAppointment(std::vector<Appointment>& apptbook){
    // calculate arguments
    apptbook.emplace_back(description, month, day, yr, hr, min);
}

这样,您就可以摆脱无用的东西:

for(int i = 0; i < apptbook.size(); i++){
    delete apptbook[i];
}
apptbook.clear();

共。

但是,如果您确实需要在这种情况下使用动态分配,则至少要学会使用std::unique_ptrstd::shared_ptr (按此顺序)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM