-
June 13th, 2012, 06:23 AM
#1
Difference between function and macro
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?
Last edited by Steve R Jones; October 1st, 2015 at 05:38 AM.
-
June 13th, 2012, 07:09 AM
#2
Re: Difference between function and macro
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...
-
June 13th, 2012, 07:13 AM
#3
Re: Difference between function and macro
Originally Posted by Yarlini Amirthanesan
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?
Email: yarlini.amirthanesan@dsto.defence.gov.au
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
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
June 13th, 2012, 07:24 AM
#4
Re: Difference between function and macro
wow. good reply.
-
June 13th, 2012, 07:43 AM
#5
Re: Difference between function and macro
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.
-
June 14th, 2012, 06:46 AM
#6
Re: Difference between function and macro
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! )
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|