473,698 Members | 2,594 Online
Bytes | Software Development & Data Engineering Community
+ 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 2356
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
4364
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 doing it - is it not? Once the application has loaded the page it is cached and is immediately available for other pages. With global variables - the best thing to do would be to use application variables - so long as there weren't too many...
10
17865
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 overwrite another copies global variable? I know that you could overwrite a value in a global variable from a function, but you could also do that if you pass the variable in and then out again... so how is that any different?
20
4486
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 structurally sound to use Application_OnStart. Am I attempting the impossible, and if so, why?
25
66076
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 layers/objects/etc... or does it expect me to create a singleton global class structure? surely it's not that terrible.
4
51177
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 a wise idea to create a clsCommonApp and let all other classes to be derived from that class? and define all constants in that base class? Any other suggestions are more than welcome. (BTW this is not for one constant, I have multiple...
8
2884
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 class that I want its instance to be accessible from any other classe in the project. There is also a bunch of constants that I want to be public for the
1
3021
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 do it this way, the scope of the constants is local--attempting to use these constants in a typical .aspx file throws an error telling me that 'NUM' is undeclared. Doing this:
6
1627
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 including config.php and declaring the variables that will be used as global. This seems very inefficient. 2.Moreover these variables are constants, is there a way to make the variables unmodifiable in config.php so that scripts that use them ,
0
1986
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 piece of software developed using VC++ 4.2, running on Windows NT 3.51 to VC++ 2005, running on Windows XP Pro SP2. I went through all of the necessary gyrations to reach the point where all of the deprecated functions were dealt with. I got a...
0
8678
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8609
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9030
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8899
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8871
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7737
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5861
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3052
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 we have to send another system
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.