Difference between enum and #define preprocessor

Discussion in 'C++' started by ashwani, Aug 11, 2006.

  1. ashwani

    ashwani Guest

    Hi
    Can any one tell me the difference between preprocessor macros like
    #define and enum.
    If i want to define MAX_LIMIT=100 as preprocessor macro as
    #define MAX_LIMIT 100
    or if i define enum {MAX_LIMIT=100}; then what is the basic difference?
    Is there any efficiency tradeoff between both the definitions.
    ashwani, Aug 11, 2006
    #1
    1. Advertising

  2. "ashwani" <> wrote in message
    news:...
    : Hi
    : Can any one tell me the difference between preprocessor macros like
    : #define and enum.
    : If i want to define MAX_LIMIT=100 as preprocessor macro as
    : #define MAX_LIMIT 100
    : or if i define enum {MAX_LIMIT=100}; then what is the basic difference?
    : Is there any efficiency tradeoff between both the definitions.

    No difference in efficiency.

    enum-declared constants are to be preferred because they
    obey scoping rules (e.g. they can be encapsulated within
    a class or namespace).

    In a modern compiler, using
    const int MAX_LIMIT = 100;
    or
    static const int MAX_LIMIT = 100;
    is also likely to be just as efficient (no space or time
    overhead) as a define.


    --
    http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
    Ivan Vecerina, Aug 11, 2006
    #2
    1. Advertising

  3. ashwani

    Rolf Magnus Guest

    ashwani wrote:

    > Hi
    > Can any one tell me the difference between preprocessor macros like
    > #define and enum.
    > If i want to define MAX_LIMIT=100 as preprocessor macro as
    > #define MAX_LIMIT 100
    > or if i define enum {MAX_LIMIT=100}; then what is the basic difference?


    enum respects scope, #define doesn't.

    > Is there any efficiency tradeoff between both the definitions.


    no.

    Btw: If you want a constant, just define a constant and not a macro or an
    enum.
    Rolf Magnus, Aug 11, 2006
    #3
  4. ashwani posted:

    > Hi
    > Can any one tell me the difference between preprocessor macros like
    > #define and enum.
    > If i want to define MAX_LIMIT=100 as preprocessor macro as
    > #define MAX_LIMIT 100
    > or if i define enum {MAX_LIMIT=100}; then what is the basic difference?
    > Is there any efficiency tradeoff between both the definitions.



    namespace MyNamespace {

    #define ONE 1

    enum { TWO = 2 };

    int const three = 3;
    }

    int main()
    {
    ONE; /* No problem */

    TWO; /* Can't find it */

    three; /* Can't find it */


    MyNamespace::ONE; /* Gibberish! */

    MyNamespace::TWO; /* No problem */

    MyNamespace::three; /* No problem */
    }

    --

    Frederick Gotham
    Frederick Gotham, Aug 11, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jakk
    Replies:
    4
    Views:
    12,049
  2. Anan H. Samiti
    Replies:
    1
    Views:
    7,590
    kaeli
    Sep 27, 2004
  3. Cronus
    Replies:
    1
    Views:
    648
    Paul Mensonides
    Jul 15, 2004
  4. Replies:
    3
    Views:
    284
    CBFalconer
    Dec 1, 2006
  5. Brian
    Replies:
    4
    Views:
    2,587
    Brian
    Feb 27, 2010
Loading...

Share This Page