且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

接受新对象的宏

更新时间:2023-11-30 17:49:52

$ p> #define EV(event)SendEvent(new event); //无法更改。

宏强制每次调用 SendEvent 应该创建一个新的动态对象。你的问题不仅仅是使用宏是愚蠢的,例如。降低了源代码的可读性。这也是宏不允许你在调用之前创建对象,并且你不能改变宏;在你的话中没有办法修改 EV 宏。






因此解决方案很简单:



不要使用宏,直接使用 SendEvent 并记住不要 delete


In my code, I have:

#define EV( event ) SendEvent( new event );
EV( evFormat );

But I want to pass a created object in the EV macro, like:

CEvent *ev = new CEvent();
EV( ev );

Is this possible? Because there is no way I will modify the EV macro.

#define EV( event ) SendEvent( new event );    // Can't be changed.

The macro enforces that each call to SendEvent should create a new dynamic object. Your problem is not just that using a macro for that is stupid and e.g. reduces the readability of your source code. It is also that the macro does not allow you to create the object earlier than the call, and that you can't change the macro; in your words "there is no way I will modify the EV macro".


The solution is therefore simple:

Don't use the macro, use SendEvent directly, and remember to not delete.