Is it really necessery to replace #define from const?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rayer

    Is it really necessery to replace #define from const?

    I have got some project src, and find it out that there is no ppl using
    "const" to define a constance, but using #define still more. Discussing
    with my friend, he said 1. #define is much easier in modify values in
    program for several propose, and 2. const takes memory but #define not.
    However, most modern textbook (for example, C++ Primal Plus 4/e) still
    suggest to use const to replace #define. I know that use #define will
    have some problem when using as macro, but that affairs to function, not
    const defination. anyone have idea why using const instead of using
    #define ? any advantage?
  • Simon Saunders

    #2
    Re: Is it really necessery to replace #define from const?

    On Sun, 20 Mar 2005 19:34:22 +0800, Rayer wrote:
    [color=blue]
    > const defination. anyone have idea why using const instead of using
    > #define ? any advantage?[/color]

    This is covered by the FAQ.



    Comment

    • qfel

      #3
      Re: Is it really necessery to replace #define from const?

      Compiler may optimize it so it is as effective as #define


      Comment

      • Igor Okulist

        #4
        Re: Is it really necessery to replace #define from const?

        "Compiler may optimize", I am afraid may is the keyword...
        quite often does not, especially for globals (like PI).

        That being said, majority still should use const.

        igor


        "qfel" <[q_tmp]@[aster.pl]> wrote in message
        news:423da19a$0 $17112$f69f905@ mamut2.aster.pl ...[color=blue]
        > Compiler may optimize it so it is as effective as #define
        >
        >[/color]


        Comment

        • msalters

          #5
          Re: Is it really necessery to replace #define from const?

          Rayer wrote:[color=blue]
          > I have got some project src, and find it out that there is no ppl[/color]
          using[color=blue]
          > "const" to define a constance, but using #define still more.[/color]
          Discussing[color=blue]
          > with my friend, he said 1. #define is much easier in modify values in
          > program for several propose,[/color]

          Eh? That doesn't make sense in English. I can't guess what it means
          so it's hard to reply.
          [color=blue]
          > and 2. const takes memory but #define not.[/color]

          Wrong. Both take memory if needed, the compiler can optimize the
          memory in both cases if not needed.
          Of course, it is possible to take the address of a const, which
          means the const will take memory. That's just an extra feature.
          [color=blue]
          > However, most modern textbook (for example, C++ Primal Plus 4/e)[/color]
          still[color=blue]
          > suggest to use const to replace #define. I know that using #define[/color]
          will[color=blue]
          > have some problem when using as macro, but that applies to function,[/color]
          not[color=blue]
          > const defination. anyone have idea why using const instead of using
          > #define ? any advantage?[/color]

          Sure. E.g. a const has a type. i.e. const std::string foo = "X";
          #define FOO "X" doesn't. The result is that passing foo to
          void bar( std::string const& ) is cheaper. Every time you call
          bar(FOO) the compiler has to convert FOO to a string, foo is
          converted only once.

          Overloading is more obvious, as it too works on types. Function
          templates that deduce types are also affected, even those as
          simple as std::find()

          Furthermore, you can have consts in functions, and they will obey
          function scope. A #define continues for the rest of the file.

          Basically, C++ was designed with const in mind. #define was
          accepted in C++ only to keep old C code working, and it
          doesn't play nice with the new features.

          HTH,
          Michiel Salters

          Comment

          Working...