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 5 2361
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
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!!!
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.
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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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?
|
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?
|
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.
|
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...
| |
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
|
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:
|
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 ,
|
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...
|
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,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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,...
|
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...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
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...
| |