如何(重新)调用一个初始化对象的构造函数? [英] How to (re)call a constructor of an initialised object?

查看:676
本文介绍了如何(重新)调用一个初始化对象的构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一些代码来检查是否插入了特定的midi设备,如果没有,代码会每5秒重新检查一次,直到插入。



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



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



类对象被声明在头文件中作为 ofxMidiIn midiIn; ,因为它在cpp文件中全局使用。问题是如果我'reeclare'在cpp中的函数中它似乎并不替换全局范围中的对象,即使它在本地很好。



用伪码解释:



在.h:

  foo {

ofxMidiIn midiIn; //第一次初始化做一个端口扫描

};

在.cpp中:

  void foo :: setup(){
midiIn.listPorts(); //如果失败,则每5秒触发重新检查
}


void foo :: run(){
//如果设置失败,则while(! recheck());
}

bool foo :: recheck(){

ofxMidiIn midiIn;
midiIn.listPorts(); // this in this(local)scope,but does not reassign midiIn worldwide

}


$

$

$

  bool foo :: recheck()
{
new(& midiIn)ofxMidiIn
midiIn.listPorts();
}

new(& midiIn)ofxMidiIn )将通过调用 ofxMidiIn 的构造函数在其自己的内存区域中重构 midiIn >。但是,如果 ofxMidiIn 有指针,并且在上一个对象中为它们分配了内存,则此方法会产生问题。你会泄漏记忆。你可以通过编写显式调用析构函数:

 (& midiIn) - >〜ofxMidiIn //显式调用析构函数
new(& midiIn)ofxMidiIn(); //然后这样做!

演示: http:/ /ideone.com/OaUtw






无论如何,我相信更好,更清洁的解决方案将是作为指针的变量:

  ofxMidiIn * midiIn; 

然后使用 new delete 。下一次执行 new 时,必须通过写为:

bool foo :: recheck()
{
delete midiIn; //必须这样做如果已经分配了内存!
midiIn = new ofxMidiIn();
midiIn-> listPorts();
}


I'm writing some code that is checking if a specific midi device is plugged in, and if it isn't the code rechecks every 5 seconds until it is plugged in.

My problem comes about in checking the list of devices - the external library has no function to re-check the ports, as it only does it in the constructor of the class.

The only way I could see of getting my code to recheck the list of devices is to re-initialize the class object.

The class object is declared in the header file as ofxMidiIn midiIn;, as it is used globally in the cpp file. The issue is if I 'redeclare' within a function in the cpp it doesn't appear to replace the object in the global scope, even though it is locally fine.

To clarify with pseudocode:

In the .h:

class foo {

    ofxMidiIn midiIn; //first initialization does a port scan

};

in the .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

}

解决方案

By using placement new you can re-call the constructor:

bool foo::recheck()
{
    new (&midiIn) ofxMidiIn();
    midiIn.listPorts(); 
}

The line new (&midiIn) ofxMidiIn() will re-construct midiIn in its own memory region, by calling the constructor of ofxMidiIn. However, this approach will create problem if ofxMidiIn has pointer(s), and you've allocated memory for them in the previous object. You will be leaking memory. You can call the destructor explicitly though, by writing as:

    (&midiIn)->~ofxMidiIn();   //call the destructor explicitly
    new (&midiIn) ofxMidiIn(); //then do this!

Demo : http://ideone.com/OaUtw


Anyway, I believe that better and clean solution would be to make the variable as pointer as:

ofxMidiIn *midiIn;

And then use new and delete. And when you do new for the next time, must delete the previous object by writing as:

bool foo::recheck()
{
    delete midiIn; //must do this if it already has been allocated memory!
    midiIn = new ofxMidiIn();
    midiIn->listPorts(); 
}

这篇关于如何(重新)调用一个初始化对象的构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆