Jump to content
  • Advertisement
Sign in to follow this  
dave

Apparently #define is not c++

This topic is 5029 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've had two lectures only this semester at uni and ive already learned alot of c++ stuff despite having done it for a year and a half. One of the things being: In C u would write: #define NUMBER 100 apparently, if your strict about standards, in C++ u would write: const unsigned int NUMBER = 100; Any one do the latter? ace

Share this post


Link to post
Share on other sites
Advertisement
Yup. Thing with #define is that it happens before compilation. For numbers it is allways better to have strong typeing but it can come in useful. For example "assert" just doesn't appear in release since it is defined in release to "nothing" (which is faster than an if statment...).

Share this post


Link to post
Share on other sites
The compiler can also [potentially] perform better optimizations with "const" than it can with "#define" because #define is handled by the pre-processor and so usually converted into a literal before the actual true compilation.

Usually, the clearer and more descriptive you are to the compiler, the better the optimizations it can make.

Share this post


Link to post
Share on other sites
Yes. The first thing you must do before writing a line of C++ is to write many lines of Java. Then come back and pretend you're using Java, instead of C with OOP tacked on. Or something equivalent - the point is to avoid using constructs that were appropriate to the C language but have been superceded by more safe or robust contructs in C++. For example, avoid using arrays - prefer Vectors unless optimisation or hardware access is necessary. Avoid pointers unless to existing, stack allocated objects. Pointers should mostly be used for polymorphism purposes, such as in containers. If you really need to use a pointer locally, consider using an auto_ptr, thus avoiding worrying about destroying the object manually along all return paths.

Now, #define is still useful, but not in its previous forms - use inlines instead of macros, and consts instead of #define constants. However, for creating meta-language or taking shortcuts on class description, templates are often inadequate, so #defines are still appropriate. You probably don't need to worry about that until you're writing templates and can see the weaknesses of the template system.

Share this post


Link to post
Share on other sites
Misleading subject.

#define is a part of C++. C++ style dictates that you should prefer consts/enums to #define. It doesn't say that #define is wrong or not standard.

Share this post


Link to post
Share on other sites
Quote:
Original post by antareus
Misleading subject.

#define is a part of C++. C++ style dictates that you should prefer consts/enums to #define. It doesn't say that #define is wrong or not standard.

Correct. There is nothing wrong, per se, with using #defines for constants. But it is considered a bad, unmodern C++ style of programming (and adding this is what Simon mentions about optimizations).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!