jmasterx
jmasterx

Reputation: 54183

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

In terms of a message system for a game, or in general, in C++, which of 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

Upvotes: 1

Views: 514

Answers (4)

Ben Voigt
Ben Voigt

Reputation: 283893

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.

Upvotes: 0

jdl
jdl

Reputation: 6333

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

Upvotes: 0

Justin Ethier
Justin Ethier

Reputation: 134255

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.

Upvotes: 2

Ord
Ord

Reputation: 5843

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.

Upvotes: 3

Related Questions