Duplicate global constants

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

    #1

    Duplicate global constants

    Hi,

    I'm linking into a library that has a global constant with the same
    name as one in my application. My references to the constant are
    being linked to the value in the library instead of the one in my
    source file. Both constants are extern so I'm assuming it's undefined
    which value I get. Obviously this is one of the reasons why global
    constants are usually a bad idea.

    I'm considering using a namespace to better encapsulate my constant
    values. However, I'm working on a huge application with many
    constants, which are used all over the place. So this would be a
    significant change. Is this the best long term solution? Are there
    other patterns for encapsulating constant values?

    Thanks,
    Will

  • RafaÅ‚ Maj Raf256

    #2
    Re: Duplicate global constants

    Will wrote:
    [color=blue]
    > I'm considering using a namespace to better encapsulate my constant
    > values. However, I'm working on a huge application with many
    > constants, which are used all over the place. So this would be a[/color]

    Using globals (especially menay of them) is considered a bad idea in C++

    --
    Wymiana starych układów... na nowe układy - prawie jak walka z korupcja.
    Walka z wychowaniem seksualnym i erotykÄ… - prawie jak walka z patologiÄ….
    PiS - prawie jak prawo i sprawiedliwoÅ›Ä ‡... Prawie. Prawie robi różnicÄ™.
    Myśl. Głosuj rozsądnie. Nie na tanie hasła. // Rafał Maj Raf256

    Comment

    • Phlip

      #3
      Re: Duplicate global constants

      Will wrote:
      [color=blue]
      > I'm linking into a library that has a global constant with the same
      > name as one in my application. My references to the constant are
      > being linked to the value in the library instead of the one in my
      > source file. Both constants are extern so I'm assuming it's undefined
      > which value I get. Obviously this is one of the reasons why global
      > constants are usually a bad idea.
      >
      > I'm considering using a namespace to better encapsulate my constant
      > values. However, I'm working on a huge application with many
      > constants, which are used all over the place. So this would be a
      > significant change. Is this the best long term solution? Are there
      > other patterns for encapsulating constant values?[/color]

      Firstly, code that only ever says "extern const int q = 5;" and never says
      "const int q = 5;" is code that relies on an old sloppy C technique that
      C++ no longer permits. 'q' must live somewhere - in a home translation unit
      - to link, even though it's constant.

      The solution is 'static const int q = 5;'. This only has scope in the
      current translation unit, and lives there, one copy per module.

      In general, your code should must use much better style (namespaces, no
      globals, etc.) to stay ahead of this legacy code.

      --
      Phlip
      http://www.greencheese.org/ZeekLand <-- NOT a blog!!!

      Comment

      • Pete Becker

        #4
        Re: Duplicate global constants

        Will wrote:[color=blue]
        > Hi,
        >
        > I'm linking into a library that has a global constant with the same
        > name as one in my application. My references to the constant are
        > being linked to the value in the library instead of the one in my
        > source file. Both constants are extern so I'm assuming it's undefined
        > which value I get. Obviously this is one of the reasons why global
        > constants are usually a bad idea.
        >
        > I'm considering using a namespace to better encapsulate my constant
        > values. However, I'm working on a huge application with many
        > constants, which are used all over the place. So this would be a
        > significant change. Is this the best long term solution? Are there
        > other patterns for encapsulating constant values?
        >[/color]

        For a short-term, minimal change, this should work:

        namespace hide_this_name
        {
        const int i = 3;
        }

        using hide_this_name: :i;

        // pre-existing code that uses "i" goes here

        Wherever your source code uses "i" the compiler will use the name
        hide_this_name: :i (in whatever form the compiler uses). That won't
        conflict with the name "i" in the library, so long as you aren't using
        any headers that refer to that version of "i".

        --

        Pete Becker
        Roundhouse Consulting, Ltd.

        Comment

        • Fei Liu

          #5
          Re: Duplicate global constants


          Will wrote:[color=blue]
          > Hi,
          >
          > I'm linking into a library that has a global constant with the same
          > name as one in my application. My references to the constant are
          > being linked to the value in the library instead of the one in my
          > source file. Both constants are extern so I'm assuming it's undefined
          > which value I get. Obviously this is one of the reasons why global
          > constants are usually a bad idea.
          >
          > I'm considering using a namespace to better encapsulate my constant
          > values. However, I'm working on a huge application with many
          > constants, which are used all over the place. So this would be a
          > significant change. Is this the best long term solution? Are there
          > other patterns for encapsulating constant values?
          >
          > Thanks,
          > Will[/color]

          Use namespace, one of its design purpose is to avoid name space
          pollution, as what's going on in your case.

          Comment

          • Tomás

            #6
            Re: Duplicate global constants

            Will posted:
            [color=blue]
            > Hi,
            >
            > I'm linking into a library that has a global constant with the same
            > name as one in my application. My references to the constant are
            > being linked to the value in the library instead of the one in my
            > source file. Both constants are extern so I'm assuming it's undefined
            > which value I get. Obviously this is one of the reasons why global
            > constants are usually a bad idea.
            >
            > I'm considering using a namespace to better encapsulate my constant
            > values. However, I'm working on a huge application with many
            > constants, which are used all over the place. So this would be a
            > significant change. Is this the best long term solution? Are there
            > other patterns for encapsulating constant values?
            >
            > Thanks,
            > Will[/color]


            Even if you don't find a solution you like, God created "Find and
            Replace" for a reason : ).


            -Tomás

            Comment

            Working...