在以下情况下实现内部聚合

Implementation of internal aggregation in the following case

本文关键字:内部 实现 情况下      更新时间:2023-10-16

我正在做的这个项目使用指针、继承和内部聚合(规则3)。我理解规则3的概念,并在这里读了一些关于它的帖子。需要实现的基本事情是有一个房间类,其中每个房间有几个会议(内部聚合)。房间有以下属性:

弦d_name

int d_nMeetings

* * d_schedule

会议。

我知道三个规则需要在Meeting** d_schedule和d_schedule中实现,d_schedule将是一个会议指针数组,它调用指向会议的指针数组。但我不太确定如何在这种情况下(在d_schedule或在下面的函数(在Meeting .h)中读取void add(Meeting*)。下面是我的代码:

Meeting.h:

#include <vector>
#include <string>
#include "item.h"
class Meeting: public Item{
protected:
  bool d_coffee;
  std::vector<std::string> d_participants;
public:
  Meeting(std::string, Time, int, bool, std::vector<std::string>, int);
  void print();
};

Meeting.cpp:

#include "meeting.h"
#include <iostream>
#include <string>
#include <vector>
#include "item.h"
Meeting::Meeting(std::string _what, Time _deadline, int _duration, bool _coffee, std::vector<std::string> _participants, int _priority = 0) :      Item(_what, _deadline, _duration, priority = 0)
{
  d_coffee = _coffee;
  d_participants = _participants;
}
void Meeting::print()
{
   Item::print();
}

Room.h:

#include "meeting.h"
class Room{
private:
  std::string d_name;
  Meeting** d_schedule;
  int d_nMeetings;
public:
  Room(std::string _name);
  void setName(std::string);
  std::string getName();
  bool add(Meeting*);
  void print();
};

谢谢

为什么不直接使用std::vector<Meeting*> d_schedule;而不是重新实现相同的功能呢?

如果你仍然想使用Meeting** d_schedule;,检查vector数据结构是如何工作的:http://codefreakr.com/how-is-c-stl-implemented-internally/

基本上你必须分配一些固定的数组并重新分配并在每次它填满时复制它