Can macro return an object?
#define macro1 {obj1}
Since macro is text substitution, could I use macro like macro1.function1()?
Thanks.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
A macro never returns anything. A macro returns textual representation of code that will be pasted into the place of the program where it is used before compilation.
Read about the C Preprocessor.
So:
… will be compiled as if you wrote
It’s just textual substitution, that can optionally take a parameter.
If you’re using GCC you can take a look at what the program will look like after preprocessing, using the
cpptool:Generally mixing macro’s with objects is bad practice, use templates instead.
One known usage of macro’s in terms of objects is using them to access singletons (however that isn’t such a nice idea in general):
You could also use the preprocessor to choose at compile time the object you want to use:
… but the forementioned templates do that better.