开发者

Should a message system use enum, string, or #define?

开发者 https://www.devze.com 2023-03-14 05:51 出处:网络
In terms of a message system for a game, or in general, in C++, which o开发者_如何学Pythonf these three is better? An enum feels best but I think it would mean newer classes could not really contribut

In terms of a message system for a game, or in general, in C++, which o开发者_如何学Pythonf these three is better? An enum feels best but I think it would mean newer classes could not really contribute. Strings seem nice for scripting advantages but I'm worried about the overhead. Integer defines feel a bit Cish.

Thanks


Why don't you make a Message class? This could contain a string to identify the message, an integer id, etc. This way you get the best of both systems. You could even have subclasses for different types of messages.


It all depends on what you are trying to do, what kind of data is in the messages (variable vs fixed-length), frequency of messaging, size of messages, etc. For example, I have seen messaging systems that use struct and union to package low level messages.

There is no one right way to do it, at least not without more information.


National Instruments Libraries and many other board level interface programs use #define for industry standards errors.


In C++, stay away from #define for simple constants. It doesn't play well with namespaces. Instead, use an enum or just a const variable in an appropriate namespace. Either of these is typesafe and properly scoped.

There are some things only #define can do, but avoid it when there's another way.

0

精彩评论

暂无评论...
验证码 换一张
取 消