如何实例化将生成并返回另一个类的对象的类的对象?

时间:2014-03-21 05:17:54

标签: c++ queue instantiation

这是实际问题。实例化给定类的对象:AirPlaneGenerator,每当间隔时间(在实例化期间设置)过去时,它将生成并返回AirPlane对象。注意:生成器将在间隔之间返回NULL;表示目前没有飞机降落。 这是机场生成器文件的cpp文件

    #include "airplanegenerator.h"
        // initialize the static list of tasks
    vector<AirPlane> AirPlaneGenerator::lstPlanes(0);
    // -- Constructors --
     // default constructor
    AirPlaneGenerator::AirPlaneGenerator(int i) {
// initialise all airplanes within the list
this->interval = i;
this->cursor = 0;
this->elapsed = 0;

     AirPlaneGenerator::lstPlanes.insert(AirPlaneGenerator::lstPlanes.end(), new      

      AirPlane(JFF_GOVERNMENT));

      AirPlaneGenerator::lstPlanes.insert(AirPlaneGenerator::lstPlanes.end(), new    
    AirPlane(JFF_MAIL));
AirPlaneGenerator::lstPlanes.insert(AirPlaneGenerator::lstPlanes.end(), new  
    AirPlane(JFF_VIP));

    AirPlaneGenerator::lstPlanes.insert(AirPlaneGenerator::lstPlanes.end(), new  
     AirPlane(JFF_MAIL));
 AirPlaneGenerator::lstPlanes.insert(AirPlaneGenerator::lstPlanes.end(), new   
     AirPlane(JFF_MAIL));
AirPlaneGenerator::lstPlanes.insert(AirPlaneGenerator::lstPlanes.end(), new   
     AirPlane(JFF_GOVERNMENT));
AirPlaneGenerator::lstPlanes.insert(AirPlaneGenerator::lstPlanes.end(), new   
     AirPlane(JFF_VIP));
AirPlaneGenerator::lstPlanes.insert(AirPlaneGenerator::lstPlanes.end(), new   
     AirPlane(JFF_VIP));
AirPlaneGenerator::lstPlanes.insert(AirPlaneGenerator::lstPlanes.end(), new 
     AirPlane(JFF_MAIL));
AirPlaneGenerator::lstPlanes.insert(AirPlaneGenerator::lstPlanes.end(), new   
     AirPlane(JFF_SMALLPASS));
AirPlaneGenerator::lstPlanes.insert(AirPlaneGenerator::lstPlanes.end(), new  
    AirPlane(JFF_SMALLPASS));
AirPlaneGenerator::lstPlanes.insert(AirPlaneGenerator::lstPlanes.end(), new 
    AirPlane(JFF_SMALLPASS));
AirPlaneGenerator::lstPlanes.insert(AirPlaneGenerator::lstPlanes.end(), new  
    AirPlane(JFF_VIP));
AirPlaneGenerator::lstPlanes.insert(AirPlaneGenerator::lstPlanes.end(), new 
    AirPlane(JFF_LARGEPASS));
AirPlaneGenerator::lstPlanes.insert(AirPlaneGenerator::lstPlanes.end(), new 
    AirPlane(JFF_MAIL));
AirPlaneGenerator::lstPlanes.insert(AirPlaneGenerator::lstPlanes.end(), new 
     AirPlane(JFF_LARGEPASS));
AirPlaneGenerator::lstPlanes.insert(AirPlaneGenerator::lstPlanes.end(), new    
    AirPlane(JFF_SMALLPASS));
AirPlaneGenerator::lstPlanes.insert(AirPlaneGenerator::lstPlanes.end(), new 
    AirPlane(JFF_GOVERNMENT));
AirPlaneGenerator::lstPlanes.insert(AirPlaneGenerator::lstPlanes.end(), new 
     AirPlane(JFF_MAIL));
AirPlaneGenerator::lstPlanes.insert(AirPlaneGenerator::lstPlanes.end(), new 
     AirPlane(JFF_LARGEPASS));
AirPlaneGenerator::lstPlanes.insert(AirPlaneGenerator::lstPlanes.end(), new 
     AirPlane(JFF_GOVERNMENT));
AirPlaneGenerator::lstPlanes.insert(AirPlaneGenerator::lstPlanes.end(), new 
     AirPlane(JFF_GOVERNMENT));
AirPlaneGenerator::lstPlanes.insert(AirPlaneGenerator::lstPlanes.end(), new 
    AirPlane(JFF_LARGEPASS));
AirPlaneGenerator::lstPlanes.insert(AirPlaneGenerator::lstPlanes.end(), new 
    AirPlane(JFF_SMALLPASS));
AirPlaneGenerator::lstPlanes.insert(AirPlaneGenerator::lstPlanes.end(), new     
    AirPlane(JFF_MAIL));
AirPlaneGenerator::lstPlanes.insert(AirPlaneGenerator::lstPlanes.end(), new 
    AirPlane(JFF_MAIL));
AirPlaneGenerator::lstPlanes.insert(AirPlaneGenerator::lstPlanes.end(), new 
    AirPlane(JFF_MAIL));
AirPlaneGenerator::lstPlanes.insert(AirPlaneGenerator::lstPlanes.end(), new 
    AirPlane(JFF_SMALLPASS));
AirPlaneGenerator::lstPlanes.insert(AirPlaneGenerator::lstPlanes.end(), new 
    AirPlane(JFF_VIP));
AirPlaneGenerator::lstPlanes.insert(AirPlaneGenerator::lstPlanes.end(), new 
    AirPlane(JFF_GOVERNMENT));
AirPlaneGenerator::lstPlanes.insert(AirPlaneGenerator::lstPlanes.end(), new 
    AirPlane(JFF_LARGEPASS));
AirPlaneGenerator::lstPlanes.insert(AirPlaneGenerator::lstPlanes.end(), new 
    AirPlane(JFF_GOVERNMENT));
cout << " GENERATOR: AirPlanes Are Ready For Landing." << endl;
}
// -- Other Interfaces --
// get the next task in the list
AirPlane* AirPlaneGenerator::getNext() {
     // find out the current elapsed time
    this->elapsed += 1;
    // return an airplane if the elapsed time exceeds the current cursor position
     if (this->elapsed / this->interval >= 1) {
    this->elapsed -= this->interval;
    if (this->cursor < TOTALPLANES) {
        cout << " GENERATOR: AirPlane# " <<   
AirPlaneGenerator::lstPlanes[this->cursor].getIdnum() << " Now Landing." << endl;
        return &(AirPlaneGenerator::lstPlanes[this->cursor++]);
    } else {
        cout << " GENERATOR: No More AirPlanes In The Air To Land." << endl;
        return NULL;
    }
    } else {
    cout << " GENERATOR: No AirPlane Landing Now." << endl;
    return NULL;
   }
}

1 个答案:

答案 0 :(得分:0)

问题是你需要实现Creational Pattern(Factory Pattern),它会在给定的间隔时间后创建类的对象。