接受新对象的宏 [英] Macro that accept new object
问题描述
在我的代码中,我有:
#define EV(event)SendEvent(new event);
EV(evFormat);
但我想传递一个创建的对象在 EV
宏,如:
CEvent * ev = new CEvent
EV(ev);
这可能吗?因为我没有办法修改 EV
宏。
$ 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
.
这篇关于接受新对象的宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!