Using #define's in C++. Your opinion...

This is a discussion on Using #define's in C++. Your opinion... within the C++ Programming forums, part of the General Programming Boards category; I have recently come across a situation where it will be beneficial to use a #define macro inside my code. ...

  1. #1
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230

    Using #define's in C++. Your opinion...

    I have recently come across a situation where it will be beneficial to use a #define macro inside my code. This addition will make the code much easier to understand and a little easier to edit later.

    The problem is that the #define's are considered legacy and are supposed to be replaced with inline functions, const variables and the like. These serve no use for me in my current situation. So the question is..

    What are everybody's opinions on using the legacy macro definition inside C++ code?

  2. #2
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Post a quick example of the macro. You're most likely best off using inline functions with templating, but it all depends on what you are using the macro for.

  3. #3
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230
    Well it seems silly. I probably won't use it in the long run (just for the time being). I have a function that takes 6 arguments and I #define a small macro for expanding a ton of arguments. Here is an example (not actually from my program):

    Code:
    #define SETA(x) 2, 4, 3, 5, x, 7
    #define SETB(x) 7, 2, 3, 5, x, 9
    #define SETC(x) 10, 2, 1, 6, x, 0
    
    int func(int i1, int i2, int i3, int i4, int i5, int i6);
    
    int someotherfunc(int iVar)
    {
        func( SETA(iVar) );
        func( SETB(iVar) );
        func( SETC(iVar) );
    }
    So I'm basically just taking advantage of the expansion capability of the macros.

  4. #4
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230
    Well I do use them in different places, however I think later on in the development of this I am going to have some other way of dealing with it.

    I just wanted to know what people's opinion's were on the idea of, like vVv said, violating "good C++ style".

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Heard about default values? Seems like they could help here to me.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. defines take up less space
    By Stan100 in forum Game Programming
    Replies: 5
    Last Post: 10-21-2003, 01:42 AM
  2. Her opinion, your opinion
    By RoD in forum C++ Programming
    Replies: 4
    Last Post: 12-22-2002, 09:50 AM
  3. A matter of opinion
    By pepperblue255 in forum C++ Programming
    Replies: 2
    Last Post: 10-05-2002, 09:51 PM
  4. Freedom of opinion
    By Shiro in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 02-10-2002, 06:06 AM
  5. opinion about books
    By clement in forum C Programming
    Replies: 7
    Last Post: 09-24-2001, 04:18 PM

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