473,388 Members | 1,370 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Creating const data

I have a variable that is initialized with a value read from a configuration
file. Once initialized, it should not be changed anywhere in my program.
What is the best way to enforce this (it should be treated as const)?
Ideally, I would like a violation to generate a compile-time error.

Many thanks.
Feb 21 '07 #1
2 1214
Andrew Chalk wrote:
I have a variable that is initialized with a value read from a configuration
file. Once initialized, it should not be changed anywhere in my program.
What is the best way to enforce this (it should be treated as const)?
Ideally, I would like a violation to generate a compile-time error.
Use a const member of a class or struct:

struct X {
const int value;

X( int v ) : value(v) {}
};

X constants( whatEver );
--
Ian Collins.
Feb 21 '07 #2
* Andrew Chalk:
I have a variable that is initialized with a value read from a configuration
file. Once initialized, it should not be changed anywhere in my program.
What is the best way to enforce this (it should be treated as const)?
Ideally, I would like a violation to generate a compile-time error.
In addition to Ian Collins' reply, you can do

Type const myVariable = valueFromConfigFile();

which for purposes of handling exceptions etc. you can put inside a
function, with lazy initialization (singleton pattern)

Type const& value()
{
static Type const theValue = valueFromConfigFile();
return theValue;
}

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Feb 21 '07 #3

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

Similar topics

6
by: Thomas Matthews | last post by:
Hi, How do I create a const table of pointers to member functions? I'm implementing a Factory pattern (or jump table). I want to iterate through the table, calling each member function until a...
21
by: Jason Heyes | last post by:
I want to allow objects of my class to be read from an input stream. I am having trouble with the implementation. Here are the different approaches I have tried: // Version 1.0 - Default...
9
by: Jack | last post by:
Hello I have a library of calculationally intensive classes that is used both by a GUI based authoring application and by a simpler non-interactive rendering application. Both of these...
7
by: nog | last post by:
What's the best approach to creating many objects of a class? I have in mind using something analogous to a table to hold the data - which is in a form similar to (char name, char address, date...
6
by: mathilda | last post by:
Help! I've been tasked to create a DSN in code and then delete it on close. For security we do not want to leave it in place outside of the application we are implementing.
4
by: musicloverlch | last post by:
I used to be able to create PDFs, automatically save them to a file, and mail them out to people. This was on Access 97 with Windows NT. We've changed to Windows XP and Access 2003. Doesn't work...
9
by: Daz | last post by:
Hello people! (This post is best viewed using a monospace font). I need to create a class, which holds 4 elements: std::string ItemName int Calories int Weight int Density
9
by: ma740988 | last post by:
Assume I have a vendor file called ' vendor.h'. Within the file there's two methods memalign and cforward. It is my understanding that the memalign function is a wrapper around malloc. cforward...
3
by: triplejump24 | last post by:
i LOVE doing c++ programming, but i never can get myself started! Once i figure it out, its fun but anyways here it goes... Im given a file c:\temp\hwk8input.txt, which contains the records of the...
5
by: randysimes | last post by:
I am making a previous program into a template to calculate all 11 options. (int, double, long double, char, etc) I believe I have the right idea, but the syntax is way off. Could you please provide...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
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,...
0
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...

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.