473,386 Members | 1,830 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,386 software developers and data experts.

use a const variable as opposed to #define - Multiple const objects created

Recently, I try to replace #define with const in header file. However,
there are concerns on, multiple const object will be created, if the
header file is included in multiple cpp files.

For example:

In version.h
------------
#ifndef VERSION
#define VERSION

#include <string>

const std::string version("alpha_0-22");

#endif
In main.cpp
------------
#include <cstdio>
#include "version.h"

extern void fun();

int main()
{
printf("address of version in main=%p\n", &version);
fun();
getchar();
}

In fun.cpp
------------
#include <cstdio>
#include "version.h"

void fun()
{
printf("address of version in fun=%p\n", &version);
}

The output of the program will be:

address of version in main=00431960
address of version in fun=00431984

It seems that two copies of version string had been created if
version.h is included in different cpp file scope. Now I am worry if
version.h file is included in thousand of cpp files, will thousand of
version string object be created?!

My alternative workaround on this is, I will let version.h declare the
version string and version.cpp define the version string.

In version.h
------------
#ifndef VERSION
#define VERSION

#include <string>

extern const std::string version;

#endif

In version.cpp
--------------
#include "version.h"

const std::string version("alpha_0-22");

Again, here is my output:

address of version in main=00431960
address of version in fun=00431960

It seems that the const string just be constructed one time only.

I am not sure whether this is the correct workaround? Or my concern on
multiple creation of const object is not an issues?

Please refer to
http://www.parashift.com/c++-faq-lit....html#faq-29.7 on why I am
using const instead of const.

Thank you for your feedback.

yccheok

May 5 '06 #1
4 2762
ya************@gmail.com wrote:
Recently, I try to replace #define with const in header file. However,
there are concerns on, multiple const object will be created, if the
header file is included in multiple cpp files.

Make them extern const.

--
Ian Collins.
May 5 '06 #2

ya************@gmail.com wrote:

I am not sure whether this is the correct workaround? Or my concern on
multiple creation of const object is not an issues?


I am not an expert, but if I remember correctly a C++ compiler is
actually allowed to optimize away the const objects, provided you don't
do anything fancy to them and/or take their address. Maybe someone can
clarify on this?

May 5 '06 #3
in*************@gmail.com wrote:
ya************@gmail.com wrote:
I am not sure whether this is the correct workaround? Or my concern on
multiple creation of const object is not an issues?

I am not an expert, but if I remember correctly a C++ compiler is
actually allowed to optimize away the const objects, provided you don't
do anything fancy to them and/or take their address. Maybe someone can
clarify on this?

It most likely will, but perhaps not with a complex object like a string.

The issue is constants declared in headers have local scope to the
compilation unit, while those declared 'extern' have global scope and
must have only one definition.

--
Ian Collins.
May 5 '06 #4
* Ian Collins:
in*************@gmail.com wrote:
ya************@gmail.com wrote:
I am not sure whether this is the correct workaround? Or my concern on
multiple creation of const object is not an issues?


I am not an expert, but if I remember correctly a C++ compiler is
actually allowed to optimize away the const objects, provided you don't
do anything fancy to them and/or take their address. Maybe someone can
clarify on this?

It most likely will, but perhaps not with a complex object like a string.

The issue is constants declared in headers have local scope to the
compilation unit, while those declared 'extern' have global scope and
must have only one definition.


I think that's a good enough explanation for the OP.

However, for the record I think it should be noted that (1) the C++
language has no notion of header versus implementation file, so the
placement of a constant here or there does not affect its linkage, and
(2) it's possible to declare and define 'extern' constants in header
files without practical problems, but due to historical reasons (the
order things were introduced in in the language) one must do that via
the template mechanism, not just declaring the constants 'inline'.

--
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?
May 5 '06 #5

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

Similar topics

10
by: b83503104 | last post by:
I know this is illegal: class XYZ { const int myConst = 1; ....}; But then, does it mean I have to use #define ? Thanks
31
by: Ben | last post by:
For many times, I've found myself changing my member variables from const back to non-const. No matter how good the original objective was, there was always at least one reason not to use const...
166
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
7
by: al | last post by:
char s = "This string literal"; or char *s= "This string literal"; Both define a string literal. Both suppose to be read-only and not to be modified according to Standard. And both have...
2
by: msaladin | last post by:
Hi all, I spent today with finding an error, I found it, though I don't no why. I have a class with static constants, like this: class EXPORT_API BusConstants { public: static const...
5
by: Bit Byter | last post by:
I have a 'root' object that serves as a container/parent for several other objects like so: class myRoot { public: myRoot(); virtual ~myRoot(); void* operator new(size_t); operator...
4
by: Rui.Hu719 | last post by:
Hi, All: I read the following passage from a book: "There are three exceptions to the rule that headers should not contain definitions: classes, const objects whose value is known at compile...
2
by: Shraddha | last post by:
When I read about volatile keyword....while explaning there was an declaration like... extern const a; But as a rule const shoule be initialised where it is defined...but we do ot initialise...
8
by: minseokoh | last post by:
Hi, Could someone explain why "const" is located after the function name and what it means? inline unsigned char *access(int off) const { if (off < 0) abort(); return (&bits_); }
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...

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.