const , static, MACRO

Discussion in 'C++' started by nilavya, Nov 11, 2005.

  1. nilavya

    nilavya Guest

    Hi Gurus,

    Since I learned C++, way back in 2002, I always had a doubt about the
    different types of storage in C++. We have "const", "static" ,
    "#define" for one or other purpose. Now I have a eVC++ application
    running on WINCE. This application has many classes, some are
    initialized dynamically and others statically. I have one class which
    has many constant values which does not change. So I have #define 'ed
    those variables. But I have read the Code Insection Guidelines, and it
    is mentioned that #define shouldnt be used in C++, instead one should
    use "const unsigned or signed". What difference does it make in using a
    value as "const unsigned" and "#define". How does this affect memory
    usage and CPU usage.

    Thanks,
    With Regards,
    Bhagat Nirav K.
     
    nilavya, Nov 11, 2005
    #1
    1. Advertising

  2. "nilavya" <> wrote in message
    news:...
    : Since I learned C++, way back in 2002, I always had a doubt about the
    : different types of storage in C++. We have "const", "static" ,
    : "#define" for one or other purpose. Now I have a eVC++ application
    : running on WINCE. This application has many classes, some are
    : initialized dynamically and others statically. I have one class which
    : has many constant values which does not change. So I have #define 'ed
    : those variables. But I have read the Code Insection Guidelines, and it
    : is mentioned that #define shouldnt be used in C++, instead one should
    : use "const unsigned or signed". What difference does it make in using a
    : value as "const unsigned" and "#define". How does this affect memory
    : usage and CPU usage.

    On today's compilers, memory or CPU usage will be the same
    (for int constants in particular).
    The key problem with #define-s is that they do not obey scope rules
    (e.g. namespaces, etc). So a const, or even an enum-definition of
    constant values, shall be preferred.

    Ivan
    --
    http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
    Brainbench MVP for C++ <> http://www.brainbench.com
     
    Ivan Vecerina, Nov 11, 2005
    #2
    1. Advertising

  3. * nilavya:
    > What difference does it make in using a
    > value as "const unsigned" and "#define".


    Macros do not respect scopes or types and have limited functionality.

    See

    <url: http://www.research.att.com/~bs/bs_faq2.html#macro>

    and

    <url: http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.7>.


    > How does this affect memory
    > usage and CPU usage.


    Usually not at all: that isn't the point.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Nov 11, 2005
    #3
  4. nilavya

    Rolf Magnus Guest

    nilavya wrote:

    > Hi Gurus,
    >
    > Since I learned C++, way back in 2002, I always had a doubt about the
    > different types of storage in C++. We have "const", "static" ,
    > "#define" for one or other purpose.


    #define has nothing at all to do with storage. It's like a simple source
    code editor built into your compiler.

    > Now I have a eVC++ application running on WINCE. This application has many
    > classes, some are initialized dynamically and others statically. I have
    > one class which has many constant values which does not change. So I have
    > #define 'ed those variables. But I have read the Code Insection
    > Guidelines, and it is mentioned that #define shouldnt be used in C++,
    > instead one should use "const unsigned or signed".


    Right. Don't use #define. Use constants. Whenever possible, avoid using the
    preprocessor.

    > What difference does it make in using a value as "const unsigned" and
    > "#define".


    #define just replaces every occurance of the specified name with what you
    write behind it on source code level, i.e. before compiling it actually
    starts.
    #define doesn't care about namespaces or scope, so your macros will clutter
    up all of them. You can't take the address of it.

    > How does this affect memory usage and CPU usage.


    With a decent compiler, it won't make a significant difference.
    Well, in some cases, a constant might be faster.
     
    Rolf Magnus, Nov 11, 2005
    #4
  5. nilavya

    nilavya Guest

    thanks for the Quick replies.

    I got the difference between #define and const.

    One final question, I have one class, and there are some variables
    which has specific values which does not change. So do I need to
    declare that variables inside the class as constant or outside the
    class. AFAIK if I declare it outside it will be global, which is not a
    good design. For example...

    class CTemp
    {
    const unsigned nSomeValue = 100;
    void SomeFunction();
    };

    const unsigned nSomeValue = 100;
    class CTemp
    {
    void SomeFunction();
    };

    And my class CTemp, is dynamically allocated when application starts,
    and is deleted when application is exited. Can any one throw some
    lights on it.. Please...

    Thanks,
    With Regards.
    Bhagat Nirav K.
     
    nilavya, Nov 11, 2005
    #5
  6. nilavya

    suresh Guest

    nilavya wrote:
    > thanks for the Quick replies.
    >
    > I got the difference between #define and const.
    >
    > One final question, I have one class, and there are some variables
    > which has specific values which does not change. So do I need to
    > declare that variables inside the class as constant or outside the
    > class. AFAIK if I declare it outside it will be global, which is not a
    > good design. For example...
    >
    > class CTemp
    > {
    > const unsigned nSomeValue = 100;
    > void SomeFunction();
    > };
    >


    It depends on how the nSomeValue related to the class. If it is bound
    to the class and not to the object (ie. It is going to be constant for
    all the objects of that class) then use "static const". And the
    syntax for initializing this is little different.
    If the nSomeValue is bound to each object (ie. It is constant for an
    object but can be different for different objects) then use "const"
    as you have declared and should be initialized during construction of
    the object.


    > const unsigned nSomeValue = 100;
    > class CTemp
    > {
    > void SomeFunction();
    > };
    >


    This is not a good idea unless it is not at all related to the class.
    (also prefer to use static if you are looking for symbalic constant
    equalant - otherwise it will occupy some memory)

    You can also use enums when you have list of realted symbalic
    constants.

    > And my class CTemp, is dynamically allocated when application starts,
    > and is deleted when application is exited. Can any one throw some
    > lights on it.. Please...
    >
    > Thanks,
    > With Regards.
    > Bhagat Nirav K.
     
    suresh, Nov 11, 2005
    #6
  7. nilavya

    nilavya Guest

    Thanks for the Extremely Fast Reply.

    Thanks,
    With Regards,
    Bhagat Nirav K.
     
    nilavya, Nov 11, 2005
    #7
  8. nilavya <> wrote:
    > class CTemp
    > {
    > const unsigned nSomeValue = 100;

    ^
    you need to put "static" keyword here, otherwise it will not compile.


    B.
     
    Bronek Kozicki, Nov 11, 2005
    #8
  9. nilavya

    Greg Comeau Guest

    In article <>,
    nilavya <> wrote:
    >Hi Gurus,
    >
    >Since I learned C++, way back in 2002, I always had a doubt about the
    >different types of storage in C++. We have "const", "static" ,
    >"#define" for one or other purpose. Now I have a eVC++ application
    >running on WINCE. This application has many classes, some are
    >initialized dynamically and others statically. I have one class which
    >has many constant values which does not change. So I have #define 'ed
    >those variables. But I have read the Code Insection Guidelines, and it
    >is mentioned that #define shouldnt be used in C++, instead one should
    >use "const unsigned or signed". What difference does it make in using a
    >value as "const unsigned" and "#define". How does this affect memory
    >usage and CPU usage.


    This may not answer every question you have, but have a look at

    http://www.comeaucomputing.com/techtalk/#definevsconst
    --
    Greg Comeau / Celebrating 20 years of Comeauity!
    Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
    World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
    Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
     
    Greg Comeau, Nov 11, 2005
    #9
    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. Rakesh Sinha
    Replies:
    4
    Views:
    1,853
    Rakesh Sinha
    Jan 13, 2005
  2. Dave
    Replies:
    10
    Views:
    35,303
    Ron Natalie
    May 22, 2005
  3. Replies:
    11
    Views:
    1,108
  4. Javier
    Replies:
    2
    Views:
    567
    James Kanze
    Sep 4, 2007
  5. er
    Replies:
    3
    Views:
    385
Loading...

Share This Page