DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8

Thread: #define

  1. #1
    Join Date
    Mar 2008
    Posts
    53

    #define

    Hello,
    I read this on thinking in c++ (on-line version pag 396) but how does should it work?
    Code:
    void f(int i) {
      i++;
    }
    #define TRACE(s) cerr << #s << endl; s
    	for (int i=0; i < 10; i++)
    		TRACE( f(i) );
    It prints "f(i)" ten times; and then?

  2. #2
    Join Date
    May 2007
    Posts
    843
    You need ask experienced programmer like danny, Jonnin.

    In my opinion, this code is not going to work because when compiler first encounter ; They just threat this as macro in single line.

    Hope help

    Can you post all code so i can debug with you together ?

    Thanks.
    Last edited by Peter_APIIT; 04-23-2008 at 05:34 AM.

  3. #3
    Join Date
    Jan 2005
    Location
    UK
    Posts
    604
    I think this code will work just fine, but probably not be doing what you expect.
    It's another prime example of how not to use macros and why not to use them.
    The point the author of TIC++ is trying to make is that the part where the function is executed is only executed *ONCE* rather than 10 times. Of course you can't see anything interesting because the function f doesn't do anything interesting. Try putting a print statement in there and run it again.
    DKyb
    -------------------------------
    Life is a short warm moment -
    Death is the long cold rest.
    Pink Floyd
    -------------------------------

  4. #4
    Join Date
    Mar 2008
    Posts
    53
    OK, f() { printf("hello\n"); } is executed only one time; and then? I'm still not the purpose of that macro 'TRACE'....

  5. #5
    Join Date
    Jan 2005
    Location
    UK
    Posts
    604
    Well the author's (Eckel) point is that seemingly correct code can do very unxepected things when you use macraos. He tries to dicourage you from using them. They are a relic from the good old C-times when things like templates etc were not available. He could have used a more obvious example to bring the point across

    Code:
    #define MY_MULT(a,b) (a * b)
    #define 
    int main()
    {
        cout << MY_MULT(3,4) << endl; // expected result
        cout << MY_MULT(2+1,1+3) << endl; // oops!!
    
        return 666;
    }
    remember: macros are expanded by the preprocessor and occurrences are textally replaced before the file is compiled.
    I would strongly dicourage the use of macros especially when you're new to C++. (And probably you should look for a better book than TIC++ as well ;-) "Effective C++" is quite good)
    DKyb
    -------------------------------
    Life is a short warm moment -
    Death is the long cold rest.
    Pink Floyd
    -------------------------------

  6. #6
    Join Date
    Mar 2008
    Posts
    53
    Are you mean this? http://www.amazon.com/Effective-Spec.../dp/0201924889
    It's seems quite different to TC++; it seems a guidelines to c++ project and don't cover more topics as TC++ do; I'll read it....
    thanks

  7. #7
    Join Date
    Jan 2005
    Location
    UK
    Posts
    604
    yep - that one. there are a few more good books out there but that gives you some insight in the perks...

  8. #8
    Join Date
    Mar 2007
    Location
    Bangalore, India
    Posts
    247
    # is the stringize operator. Basically, it prints the expression itself instead of the output. ie, instead of printing the VALUE of f(i), it prints the string f(i) itself.

    int x = 10;
    TRACE(x+1);

    prints
    x+1

    and not
    11

    I hope this is what you were looking for.

Similar Threads

  1. Cellphone Text (SMS) Editor part 1
    By watdg in forum C++
    Replies: 5
    Last Post: 03-12-2008, 04:56 AM
  2. Need Help for a hook subclass issue in Word
    By hcadieu in forum VB Classic
    Replies: 0
    Last Post: 02-14-2006, 01:39 AM
  3. VB/C Array parameters
    By Gastao Woelfert in forum VB Classic
    Replies: 2
    Last Post: 09-01-2000, 11:36 AM
  4. VB/C Array parameters
    By Gastao Woelfert in forum VB Classic
    Replies: 0
    Last Post: 09-01-2000, 08:59 AM
  5. Adding Columns to comboboxes through API
    By Lori Taylor in forum VB Classic
    Replies: 20
    Last Post: 06-14-2000, 11:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center