#### Archived

This topic is now archived and is closed to further replies.

# Plain Question: DO or DO NOT...

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

## Recommended Posts

What''s the best way to declare cosntant variables? (how can that be ) const float Pi 3.1415xxxxxxxf or #define Pi 3.1415xxxxxxf

##### Share on other sites
I think the const would be better if you are using C++ and #define if you are using C.

##### Share on other sites
use #define only if you can''t do it in const, like:
#define MAX(a, b) (((a) > (b)) ? (b) : (a))

Press any key to continue or any other key to quit...

##### Share on other sites
Simple rule of thumb ...

1. If there is something different in C++ than C, it serves a purpose and it''s purpose and expected usage are documented somewhere - and probably in the standard document or in stroustroup''s book "The C++ Language" (unless it''s a question about the standard library).
a. If the purpose is explained in the standard itself, in Bjarne Stroustroup''s book, or in a book or article by any of the following: P.J. Plauger, Stanley Lipman, Herbert Shildt, Scott Meyers or Andrew Koenig than it is probably valid (there are other''s but my memories not perfect).
b. If the reason is explained here on this forum, than read the explanation very carefully and decide for yourself, because most people here are just repeating what they''ve been told, without real understanding.
c. If someone just says the C way is better because it''s simpler or more efficient, they are NOT doing you a favor ... becasue they are either wrong or if they are right they are refusing to tell you WHY it''s better and more efficient .. which hurts you in the long run.
d. If someone just says the C++ way is better because it''s modern or object-oriented they are also not helping you at all. Saying something is more or less modern or object-oriented is not an excuse to be used - object-oriented programming is a method of viewing a programs parts that CAN help make the design of a program cleaner ... but it is not always better, nor is it even what C++ is all about ... C++ also embodies improved generic programming, improved functional programming, improved dynamic programming, and improved type safety over C. All of which help only when used and used well (there is no "correct" but there is "incorrect" usage).

Consts are better than #defines in the places they are applicable for one important reason - they provide type information to the complier ... which is one of C++''s main goals, improved verifiability through better typing and constant mechanisms. Some less important considerations: Consts will not replace all defines because they cannot express macros (which is a good job for inline functions) ... Consts are also good because there is no equivelent to #undef ... which means a poorly written file can''t change the value behind your back ... the const version will also tell you when it is being cast to a potentially incompatible type. The only time I know of when the #define version is preferable is when you are rapidly prototyping a system and warnings about types which you might still change would just waste your time ... and then when you define the exact needs of the variable you will make it a const and then clear up any missed warnings properly ... that way you wont have any unnoticed casts lingering in your code to bite you latter).

I strongly advise you buy the Stroustroup book ... it explains so many usefull things ... including times to use so many of the C++ language features ... and times NOT to.

Good Luck

##### Share on other sites
MindFlayer, I think you mean:

  #define MAX(a, b) (((a) > (b)) ? (a) : (b))

But this is better if your using C++ due to the type-checking that Xai mentions.

  templateinline const t& MAX(const t& a, const t& b){return a > b ? a : b}

This function works for things like:
  MAX(a++,b);

Whereas the original #define would do funky things depending on if a or b was larger.

Plus this will work for any class where the > operator has been overloaded.

1. 1
Rutin
25
2. 2
3. 3
JoeJ
19
4. 4
5. 5

• 10
• 11
• 9
• 9
• 10
• ### Forum Statistics

• Total Topics
631752
• Total Posts
3002095
×