在以下情况下实现内部聚合
Implementation of internal aggregation in the following case
我正在做的这个项目使用指针、继承和内部聚合(规则3)。我理解规则3的概念,并在这里读了一些关于它的帖子。需要实现的基本事情是有一个房间类,其中每个房间有几个会议(内部聚合)。房间有以下属性:
弦d_nameint 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/
基本上你必须分配一些固定的数组并重新分配并在每次它填满时复制它
相关文章:
- 如何在跳过所有内部实现的同时跳转到 GDB 中 std::function 中的函数?
- 在命名空间内部还是外部实现 c++ 函数?
- 对象内部有大量数据容器,实现更高效的对象交换
- C 无需使用匿名名称空间即可实现内部链接
- 内部源代码 - 在链接列表实现中插入元素上的删除会破坏整个应用程序
- C++ - 在单独的文件中实现内部模板类的模板方法
- 如何在c++中实现内部抽象成员类
- 内部分布式时间服务器实现
- friend函数是如何在内部实现的
- 为具有内部指针的对象实现swap()
- 使用STL内部实现的红黑树
- 枚举在内部作为整数实现.那为什么会给出错误
- 内部套接字接收缓冲区实现
- std::vector 实现是否使用内部数组或链表或其他
- STL::MAP在C++中的内部实现
- 通过内部指针隐藏实现详细信息
- 如何实现将数据从用户应用程序传输到类内部存储器的类方法
- STL堆栈和队列的内部实现
- 如何实现内部实现依赖于模板参数的类
- Switch case语句如何在内部实现或工作