473,473 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

Mar 1 '06 #1
5 2344
Will wrote:
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


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
Mar 1 '06 #2
Will wrote:
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?


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!!!
Mar 1 '06 #3
Will wrote:
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?


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.
Mar 1 '06 #4

Will wrote:
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


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

Mar 1 '06 #5
Will posted:
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

Even if you don't find a solution you like, God created "Find and
Replace" for a reason : ).
-Tomás
Mar 1 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: mark4asp | last post by:
What are the best methods for using global constants and variables? I've noticed that many people put all global constants in a file and include that file on every page. This is the best way of...
10
by: Matt | last post by:
Greetings, What are people's thoughts on global variables in C++? Why are we taught not to use them in programming? Is it true that if you are running two copies of the C program one copy can...
20
by: 2obvious | last post by:
I've been trying to create read-only global variables by creating constants (Const) in my global.asa, but I can't seem to reference them. Sticking them in an include works fine, but it seems more...
25
by: Daniel Bass | last post by:
how do i declare a global variable in c#.net? it's like it want's everything in classes... there are times when globals are good, like having constants in a program which apply to several...
4
by: Amadelle | last post by:
Hi all and thanks again in advance, What is the best way of defining global constants in a C# application? (A windows application with no windows forms - basically a set of classes). Would it be...
8
by: Marty | last post by:
Hi, I'm new to C#, I used to code in VB.NET. Where is the best place to declare all my constants and global objects in my C# project to have them accessible globally? I have an event logger...
1
by: 2obvious | last post by:
I want to declare some constants on the application level in global.asax to use throughout my application, e.g.: Sub Application_OnStart() Const NUM As Integer = 5 End Sub Problem is, when I...
6
by: lazy | last post by:
hi, I have some constants defined in a php script say config.php. I want to use the variables there defined in other scripts. couple of questions regd that: 1. Is there an alternative to...
0
by: =?Utf-8?B?UGF1bCBIYWdlcg==?= | last post by:
I've been trying to solve this issue for the better part of a month. My attempts to get an answer on the MSDN groups proved to no avail. Here is the situation/problem. I am migrating an old...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.