renaming functions::prepocessor?

This is a discussion on renaming functions::prepocessor? within the C++ Programming forums, part of the General Programming Boards category; How would i go about using the preprocessor directives to supply an alternate name to an existing function. I'm sort ...

  1. #1
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001

    renaming functions::prepocessor?

    How would i go about using the preprocessor directives to supply an alternate name to an existing function. I'm sort of on an anti hungarian mission. Thanks.
    PHP and XML
    Let's talk about SAX

  2. #2
    booyakasha
    Join Date
    Nov 2002
    Posts
    208
    #define BLAH(x,y) ( foo(x,y) )


    int foo(int a , int b);


    int main(){

    int q = BLAH( 4 , 4 );

    }
    int foo..........................


    I think that sure work, but I'm not positive

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    This would require memory space but an alternative would be to use a pointer.

    Example
    Code:
    int foo(int, int);
    int (*bar)(int, int) = foo;

  4. #4
    booyakasha
    Join Date
    Nov 2002
    Posts
    208
    Originally posted by Salem
    There's no need to include all the parameters

    #define nicename pscxacSomeDumbHungarianName
    That's true but I think that defining it as a function is better because otherwise the preprocessor will change every occurance of the word even if it isn't being used as a function

    like if you did

    #define mult fooFunction

    int fooFunction(int x, int y) { return x * y; }

    int main() {

    int multResult = mult(5,5);

    }


    the word mult in multResult will also be changed to fooFunction
    so the int will be called fooFunctionResult

    while in most cases the program will function the same either way, I think there will be some rare cases where there will be problems.

  5. #5
    booyakasha
    Join Date
    Nov 2002
    Posts
    208
    I tested and found out that my previous post was partially wrong, my compiler ( and I guess other ) will only rename if the word appears alone

    so

    #define mult foo

    will change

    int mult = mult(3,4);

    to

    int foo = foo(3,4);

    but

    int multResult = mult(3,4);

    will only change to

    int multResult = foo(3,4);

    but I would still recommend using

    #define mult(x,y) foo(x,y)
    to avoid any problems ( and hey it's only a few extra chars anyway ).

  6. #6
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    I'd say this is the best solution:
    Constant function pointers, no extra memory used.
    Code:
    int (* const sys)(const char *) = std::system;
    
    main()
    {
      sys("dir");
    }
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  7. #7
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    great, thanks guys, i'll probably go with the constant functions pointers.
    PHP and XML
    Let's talk about SAX

  8. #8
    booyakasha
    Join Date
    Nov 2002
    Posts
    208
    Originally posted by Sang-drax
    I'd say this is the best solution:
    Constant function pointers, no extra memory used.
    using #define doesn't change the amount of memory used.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. renaming executable file causes access violation
    By m37h0d in forum C++ Programming
    Replies: 8
    Last Post: 05-29-2008, 10:41 AM
  2. Renaming protection
    By NoFearXD in forum Windows Programming
    Replies: 3
    Last Post: 05-03-2007, 04:34 PM
  3. Help on renaming files using a C program
    By sangken in forum C Programming
    Replies: 21
    Last Post: 08-03-2006, 06:30 PM
  4. renaming file without rename() function?
    By subodh_dg in forum C Programming
    Replies: 3
    Last Post: 03-26-2005, 09:40 AM
  5. Question about renaming a File
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 01-14-2002, 05:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21