相关疑难解决方法(0)

如何(重新)调用初始化对象的构造函数?

我正在编写一些代码,用于检查是否插入了特定的midi设备,如果不是代码,则每隔5秒重新检查一次,直到插入为止.

我的问题出现在检查设备列表中 - 外部库没有重新检查端口的功能,因为它只在类的构造函数中执行.

我能看到让我的代码重新检查设备列表的唯一方法是重新初始化类对象.

类对象在头文件中声明为ofxMidiIn midiIn;,因为它在cpp文件中全局使用.问题是,如果我在cpp中的一个函数内'重新声明',它似乎不会替换全局范围内的对象,即使它在本地很好.

用伪代码澄清:

在.h:

class foo {

    ofxMidiIn midiIn; //first initialization does a port scan

};
Run Code Online (Sandbox Code Playgroud)

在.cpp中:

void foo::setup(){
    midiIn.listPorts(); //if this fails the recheck is triggered every 5 secs
}


void foo::run(){
    //if setup failed then while (!recheck());
}

bool foo::recheck(){

    ofxMidiIn midiIn;
    midiIn.listPorts(); //this works in this (local) scope, but doesn't reassign midiIn globally

}
Run Code Online (Sandbox Code Playgroud)

c++ constructor scope initialization

4
推荐指数
1
解决办法
4818
查看次数

标签 统计

c++ ×1

constructor ×1

initialization ×1

scope ×1