Hello expects. Can you tell me wot is the difference between a function and macro in C++. They lok the sam. Can you tell? When you use a macro and not a function? :o
Printable View
Hello expects. Can you tell me wot is the difference between a function and macro in C++. They lok the sam. Can you tell? When you use a macro and not a function? :o
A macro replaces the original text in the code before the compiler start to process the file. I.e. every single place where you use the macro will get a duplicate of the code.
A function is a complete section of code i.e. no duplication occur.
Consider macros to be evil and stick to using functions instead, at least until you have a LOT of programming experience. There is no end in how many hard to solve issues you can create for yourself with the use of macros...
No: From the call alone, it is not possible to know if you are using a macro, a function, or an object's operator().
However, there is a very widespread coding practice that says that macros should be in ALL_CAPS:
Code:DO_SOMETHING(); //Probably a macro
do_something(); //Probably a function
wow. good reply.:wave:
In 99% of cases, inline functions are superior to macros.
The one thing which macros can do that functions can't, is to take complex statements as arguments. Usually, this just obscures things, but from time to time it can be useful.
Another downside to macros is that they don't obey the C++ scoping rules.
ANYTHING that matches the macro name is replaced by the pre-processor.
Anybody who has named a member function GetMessage when using Windows and MFC will know what I mean.
(The preprocessor will replace ANY GetMessage with GetMessageA or GetMessageW, depending on whether the setup is ASCII or Unicode. The compiler will then complain that your class doesn't contain a definition of one of these! :sick:)