Results 1 to 4 of 4

Thread: Basic C++ doubts

  1. #1
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Basic C++ doubts

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Basic C++ doubts

    Quote Originally Posted by munna
    Can someone please tell me why x is incremented twice and f() is called twice when i use unsafe(x++) and unsafe(f()) respectively ?
    Because:
    Qt Code:
    1. ans = unsafe(f());
    To copy to clipboard, switch view to plain text mode 
    will be changed by the preprocessor to:
    Qt Code:
    1. ans = ( (f()) >= 0 ? (f()) : -(f()) );
    To copy to clipboard, switch view to plain text mode 
    before the compilation.

    Quote Originally Posted by munna
    I think #define's are not needed at all but I am not able to figure out how to do away with them.
    Use virtual method that will return the type of the node. If each node would know what to do with its children you wouldn't need any casts.

  3. #3
    Join Date
    Mar 2006
    Location
    Mountain View, California
    Posts
    489
    Thanks
    3
    Thanked 74 Times in 54 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Basic C++ doubts

    Do no mistake macros for functions. Preprocessor macros get expanded, not evaluated. The difference can be significant, as you have found out.

  4. #4
    Join Date
    Feb 2006
    Location
    US
    Posts
    173
    Thanks
    16
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Basic C++ doubts

    Good point, Brandybuck. I generally try to avoid macros altogether because of being burned in the past so many times.

Similar Threads

  1. Some critical "QtWebKit" doubts
    By crazymoonboy in forum Newbie
    Replies: 1
    Last Post: 18th September 2008, 23:56
  2. Where can I get basic WebKit software?
    By crazymoonboy in forum General Discussion
    Replies: 1
    Last Post: 4th September 2008, 14:15
  3. Please Help-->have few doubts with Static build in Windows !
    By Krish in forum Installation and Deployment
    Replies: 1
    Last Post: 17th March 2008, 14:37
  4. Need Basic html Browser
    By awalesminfo in forum Newbie
    Replies: 6
    Last Post: 21st March 2006, 17:14
  5. Basic question on new and delete
    By jcr in forum General Programming
    Replies: 25
    Last Post: 14th February 2006, 15:09

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.