2015-10-19 77 views
0

我正在使用這個項目使用指針,繼承和內部聚合(3的規則)。我理解3的規則的概念,並在這裏閱讀了幾篇關於它的文章。需要實施的基本原則是每個房間都有幾個會議(內部聚合)。客房具有以下屬性:C++ - 在下列情況下實現內部聚合

串d_name

INT d_nMeetings

會議** d_schedule。

我知道三個規則需要在Meeting ** d_schedule和d_schedule中實現,它是一個會議指針數組,它調用指向會議的指針數組。 但是我不太確定它在這種情況下的工作方式(在d_schedule中或在下面的函數中(在meeting.h中),它讀取void add(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(); 
}; 

謝謝

回答

0

爲什麼不只是使用std::vector<Meeting*> d_schedule;而不是重新實現相同的功能。

如果你還是想用Meeting** d_schedule;,結賬vector數據結構是如何工作的: http://codefreakr.com/how-is-c-stl-implemented-internally/

基本上你就必須分配一些固定陣列和重新分配,並在它每次它填補了一次複製。

相關問題