I have to calculate the number of times a method is called. So I have a macro A initially set to 0. Can I increment it with my particular method like A++ and return it in the main method.Or how would I use the COUNTER macro.
Asked
Active
Viewed 741 times
-1
-
Are you sure you want to use a macro? Even a global variable would be better. – Beta Oct 07 '13 at 03:41
-
I recommend setting a global variable like beta said, macro isn't necessary for this kind of stuff. – Domecraft Oct 07 '13 at 03:44
-
It can't be done with macros! – Wagner Patriota Oct 07 '13 at 03:46
-
macro is not a variable, how can it store values? for counting a static local variable is enough – phuclv Oct 07 '13 at 04:32
2 Answers
0
I have a macro A initially set to 0. Can I increment it with my particular method like A++ ?
No ++ needs a lvalue, you cannot do 0++, as A
will be replaced as its value after per-processing.
Why not simply make it as global, or use call by reference ?

P0W
- 46,614
- 9
- 72
- 119
-
I have no idea about c++ programming. how do i make a global reference – user2852227 Oct 07 '13 at 03:48
-
1@user2852227: Get [a decent C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list?rq=1). – MSalters Oct 07 '13 at 08:31
0
A macro is only processed by the preprocessor, which is only run at compilation and has no knowledge of how many times a function is actually called. As Beta said, you can use a variable to store this count, but even that needs to be done carefully if you have multiple threads calling the method.

Jamie
- 76
- 3