Qt Code:
  1. // A macro that returns the absolute value of i
  2. #define unsafe(i) \
  3. ( (i) >= 0 ? (i) : -(i) )
  4.  
  5. // An inline function that returns the absolute value of i
  6. inline
  7. int safe(int i)
  8. {
  9. return i >= 0 ? i : -i;
  10. }
  11.  
  12. int f();
  13.  
  14. void userCode(int x)
  15. {
  16. int ans;
  17.  
  18. ans = unsafe(x++); // Error! x is incremented twice
  19. ans = unsafe(f()); // Danger! f() is called twice
  20.  
  21. ans = safe(x++); // Correct! x is incremented once
  22. ans = safe(f()); // Correct! f() is called once
  23. }
To copy to clipboard, switch view to plain text mode 

Can someone please tell me why x is incremented twice and f() is called twice when i use unsafe(x++) and unsafe(f()) respectively ?

I also have an other doubt also.

I build a tree from a string. For Example: "a+bc+34" or "a+2+4"

I have two classes for this.

1.Literal
2.Value

and they inherit a class called "Node" which has a variable called "type : int"

Also I have two macros

#define LITERAL_OBJECT
#define VALUE_OBJECT

which helps me to find what type of node is and then type cast and then do some operation ( addition when the two nodes under plus are numbers...etc)

I think #define's are not needed at all but I am not able to figure out how to do away with them. Can someone pls tell me how can i do things in much better way?

Thanks a lot.