src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
What this topic is about?
Its about the usage of three keywords in C++, a general guide on when,where and how and how not to use them.
const: It is a keyword which means the variable after it will be a constant.
const int a = 10;
If there is a declaration like this, then the value of 'a' will continue to be 10 through the scope of the variable irrespective of what you assign to it after this.
Thus
const int a =10; a =15; printf("%d", a); |
will print 10 and not 15
inline: This keyword in used to tell the compiler that the following function should be treated specially by the compiler which makes it more efficient. But there is a loss of predictability because some compilers treat this as a normal function and still other will treat it as a normal function only in memory intensive situation.
#define: This is a keyword which is used extensively by amateur programmers because on the face it looks a very cute, innocent and helpful keyword. But in actual it creates more problems than it solves......
eg:
#define INTEREST_RATE 10
After this definition the whole code will be process by the preprocessor and where ever it finds INTEREST_RATE, it will be replaced by the value 10. Thus it is called a preprocessor directive. Forget run time, it happens before the code is compiled.
Here you have very well used the #define to be convenient for you.Advantages:1. It is very readable2. It can be changed at one place and the change will be reflected throughout the programDisadvantages:1. Did you notice you are actually using it in an expression. Here the bug factor is a bit mitigate because I have initially used a variable and assigned it the value of the constant. However you are using a mathematical expression and expect everything to be in float. did you just now notice that "RATE_OF_INTEREST " is not a float. The compiler will just replace wherever it finds RATE_OF_INTEREST with its corresponding value it sees at the top.Consider the other dangerous way it could go wrong:
#define RATE_OF_INTEREST 8.5 int main() { int a = RATE_OF_INTEREST ; } |
Now of course you get the .5 truncated. But is there any way you can typecast it. No. You are simply using a constant. You cannot typecast a constant. You have to live with the hell you just now created for yourself.HOW #DEFINE SCREWS UP DEBUGGING:In a big program as say windows or other such big software, you wont be debugging using the good old visual studio. So you depend like hell on symbols. Yes tell a person doing some debugging that we dont have symbols he will go mad. Here you will tell him that we have screwed up symbols he will commit suicide. Yes that is what you did just now. You created a screwed up symbol for your code by using #define. Here is how it gets screwed:You will see RATE_OF_INTEREST in your source code but the symbols will not contain this variable. It will simply contain the value 8. whatever you have defined it to be. And you end up guessing why you are not able to trace the variable.
The expression
int a = RATE_OF_INTEREST ;
will appear in the symbol file as
int a = 8.5;
#DEFINE FUNCTIONSL: God that is the Den of bugs. They are good only if you want to ask some interview question but never ever include a #define function in your code. The best example is
#define max(a,b) ((a) > (b) ? (a) : (b)) // I havnt put all those braces for no reason, try to figure out :) int a = 5, b = 0; max(++a, b);max(++a, b+10); |
Compile it and I bet you cant predict the result.WHAT TO REPLACE IT WITH:Simple !!! use inline functions for #define functions and const global variables for #define variables.But dont forget the limitations which you have to overcome.
1. const char * const authorName = "Scott Meyers" can replace
#define AUTHOR_NAME "Scott Meyers".
It is a constant character pointer to constant string. You can neither play with the string nor with the pointer. Both are constant.
2. const int RATE_OF_INTEREST cannot replace the #define discussed previously!!!!!beacause it cannot handle float values then. Lesson: Try to be type safe by using as generic type as possible and make sure you declare it as constant or else you end up in the famous global variable black hole
3. inline int max(int a, int b) { return a > b ? a : b; } can replace the #define function mentioned above. Again you are forgetting what you learnt a couple of lines back. To make it type safe. Can it handle float. How to make it handle both float and int. This is needed because you may very well need functions where you are exploiting the truncation property of int. Say int a/3 will return 2 if a= 8. For this use template. A discussion of templates will end up in volumes so let us leave that for now.Still you will get some instances where you will find situations where you have to use #define statements. use them considering that you have put your neck inside the noose. One slip and you are gone. But you can always avoid that one slip :)