Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/159.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将任何对象传递给函数以创建此对象 我用OpenFrasWork+C++制作了一个小型的生成视频分层软件。为此,我有一个主类、一个层类和许多不同类型的视频/模拟类。我需要这样做,当我按下一个键,一个不同的模拟加载_C++_Oop_Openframeworks - Fatal编程技术网

将任何对象传递给函数以创建此对象 我用OpenFrasWork+C++制作了一个小型的生成视频分层软件。为此,我有一个主类、一个层类和许多不同类型的视频/模拟类。我需要这样做,当我按下一个键,一个不同的模拟加载

将任何对象传递给函数以创建此对象 我用OpenFrasWork+C++制作了一个小型的生成视频分层软件。为此,我有一个主类、一个层类和许多不同类型的视频/模拟类。我需要这样做,当我按下一个键,一个不同的模拟加载,c++,oop,openframeworks,C++,Oop,Openframeworks,我有两个问题: 如何访问所有模拟类的列表 接下来,我如何将此列表中的一个选项输入到可以接受任何类型对象并启动它的函数中 我画了一张图,而不是用括号来编码,使所有3个块位于同一水平面上(实际代码在下面) 为清晰起见,以下是代码中的main.cpp和layer.cpp: // main.cpp void ofApp::setup(){ layer1.setup(); layer2.setup(); layer3.setup(); } void ofApp::update(

我有两个问题:

  • 如何访问所有模拟类的列表
  • 接下来,我如何将此列表中的一个选项输入到可以接受任何类型对象并启动它的函数中
  • 我画了一张图,而不是用括号来编码,使所有3个块位于同一水平面上(实际代码在下面)

    为清晰起见,以下是代码中的main.cpp和layer.cpp:

    // main.cpp
    void ofApp::setup(){
        layer1.setup();
        layer2.setup();
        layer3.setup();
    }
    
    void ofApp::update(){
        layer1.update(); 
        layer2.update(); 
        layer3.update(); 
    }
    
    void ofApp::draw(){
        layer1.draw(); 
        layer2.draw(); 
        layer3.draw(); 
    }
    
    void ofApp::keyPressed(int key){
        switch (key)
        {
        case '1':
            // get first class from list of classes
            layer1.changeSim(); // input first class from list of simulation classes
            break;
        default:
            break;
        }
    }
    
    和layer.cpp

    void layer::setup(){
    simulation = new Simulation();  // how do i initialize the Simulation variable if I dont know what type itll be ?
    }
    
    void layer::update(){
        simulation.update(); 
    }
    
    void layer::draw(){
        simulation.draw(); 
    }
    
    void layer::changeSim(){
        simulation = new Simulation(); // destroy old simulation and create new one, but will be of a different class
    }
    

    实现这一点的最佳方法是使用称为factory模式的东西

    基本上,定义一个函数,该函数接受某个已定义类型的参数,该参数可用于确定所需的模拟类型,然后创建该模拟并返回指向该模拟的指针

    它的简单版本可能如下所示:

    enum SimulationType
    {
        Simulation_None,
        Simulation_Basic,
        Simulation_Complex,
    ...
    };
    
    Simulation* CreateSimulation(SimulationType Type)
    {
        switch(Type)
        {
        case Simulation_None:
        default:
            return nullptr;
        case Simulation_Basic:
            return new BasicSimulation();
        case Simulation_Complex:
            return new ComplexSimulation();
    ...
        }
    }
    
    ...
    
    void ofApp::keyPressed(int key){
        switch (key)
        {
        case '1':
            // get first class from list of classes
            layer1.changeSim(Simulation_Basic); // input first class from list of simulation classes
            break;
        default:
            break;
        }
    }
    
    ...
    
    void layer::setup(){
    simulation = CreateSimulation(Simulation_None);
    }
    
    void layer::changeSim(SimulationType Type){
        if(simulation) delete simulation;
        simulation = CreateSimulation(Type);
    }
    
    

    这是一个简单的示例-您还可以使用一个由
    SimulationType
    索引的表,这是一个
    SimulationType
    的有序映射,它引用每种类型的静态构造函数,基本上是将标识符与构造函数关联起来的任何东西。

    这听起来很适合:为每个接受泛型参数对象的模拟类注册构造函数。然后,您可以按名称查找构造函数并调用它以返回实例化的模拟对象。非常感谢您的快速回答!目前正在阅读所有这些,非常感谢!:)但有一件事我有点难以理解。所以我在我的图层类中创建了一个像你提到的Simulation*CreateSumulation(SimulationType类型)函数。我得到的输入来自枚举,但输出类型为Simulation*是udnefined,layer::setup()和layer::changeSim中的“Simulation”变量也是udnefined,我如何为它们定义一个类型?因为有许多可能的模拟类。为了得到它,我将它们声明为
    static layer*CreateSimulation(模拟类型);
    层*模拟;
    无论各种模拟的基类是什么,都是您想要使用的类型。这样您可以从它继承来定制您的行为。等等,这对我来说没有意义,为什么CreateSimulation的返回会是Layer类型,而我们希望它是模拟的类型我已经成功地实现了你开发的所有东西,但是我的返回对象是Layer类型的,当我在Layer.cpp setup()中调用simulation.setup()时,会产生堆栈溢出功能
    层中的
    模拟
    表示用于该特定层的模拟,还是表示当前层下的层?