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

Const objects defined in headers

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
time, and inline functions are all defined in headers. "

Can someone explain to me why some of the const objects must be defined
in the header file?

Thanks in advance for the help

--Rui

Aug 8 '06 #1
4 3255
* Ru*******@gmail.com:
>
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
time, and inline functions are all defined in headers. "

Can someone explain to me why some of the const objects must be defined
in the header file?
No, because (1) that's not what the quoted passage says, and (2) the C++
standard does not assume the existence of files or a file system.

As a practical matter you can define const objects in a header file.

That doesn't mean you must.

As a formal matter you can define anything in a header file.

That doesn't mean you must or should.

--
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?
Aug 8 '06 #2
Hi, Alf:

Thanks a lot for the information, I am still a little confusing here,
bear with me since I am new to C++. Here is the complete section from
the book, basically it is saying that you should never put definitions
for variables or functions in header files since headers are included
in multiple source files, but for some reason, the definition for some
constants should in the header file.

from the book, C++ Primer:

"When designing a header it is essential to remember the difference
between definitions, which may only occur once, and declarations, which
may occur multiple times, Because headers are included in multiple
source files, they should not contain definitions of variables or
functions."

"There are three exceptions to the rule that headers should not contain
definitions: classes, const objects whose value is known at compile
time, and inline functions are all defined in headers. These entities
may be defined in more than one source file as long as the definitions
in each file are exactly the same.

These entities are defined in headers because the compiler needs their
definitions (not just declarations) to generate code. For example, to
generate code that defines or uses objects of a class type, the
compiler needs to know what data members make up that type. It also
needs to know what operations can be performed on these objects. The
class definition provides the needed information. That const objects
are defined in a header may require a bit more explanation."

Recall that by default a const variable (Section 2.4, p. 57) is local
to the file in which it is defined. As we shall now see, the reason for
this default is to allow const variables to be defined in header files.

In C++ there are places where constant expression (Section 2.7, p. 62)
is required. For example, the initializer of an enumerator must be a
constant expression. We'll see other cases that require constant
expressions in later chapters.

Generally speaking, a constant expression is an expression that the
compiler can evaluate at compile-time. A const variable of integral
type may be a constant expression when it is itself initialized from a
constant expression. However, for the const to be a constant
expression, the initializer must be visible to the compiler. To allow
multiple files to use the same constant value, the const and its
initializer must be visible in each file. To make the initializer
visible, we normally define such consts inside a header file. That way
the compiler can see the initializer whenever the const is used.

However, there can be only one definition (Section 2.3.5, p. 52) for
any variable in a C++ program. A definition allocates storage; all uses
of the variable must refer to the same storage. Because, by default,
const objects are local to the file in which they are defined, it is
legal to put their definition in a header file.

There is one important implication of this behavior. When we define a
const in a header file, every source file that includes that header has
its own const variable with the same name and value.

When the const is initialized by a constant expression, then we are
guaranteed that all the variables will have the same value. Moreover,
in practice, most compilers will replace any use of such const
variables by their corresponding constant expression at compile time.
So, in practice, there won't be any storage used to hold const
variables that are initialized by constant expressions.

When a const is initialized by a value that is not a constant
expression, then it should not be defined in header file. Instead, as
with any other variable, the const should be defined and initialized in
a source file. An extern declaration for that const should be made in
the header, enabling multiple files to share that variable.

I don't really understand the explanation offered by the book.

Thanks again for the help

--Rui


Alf P. Steinbach wrote:
* Ru*******@gmail.com:

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
time, and inline functions are all defined in headers. "

Can someone explain to me why some of the const objects must be defined
in the header file?

No, because (1) that's not what the quoted passage says, and (2) the C++
standard does not assume the existence of files or a file system.

As a practical matter you can define const objects in a header file.

That doesn't mean you must.

As a formal matter you can define anything in a header file.

That doesn't mean you must or should.

--
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?
Aug 8 '06 #3
* Ru*******@gmail.com:
[top-posting]
Please don't top-post in this group. See the FAQ. Corrected.
* Ru*******@gmail.com:
[snip]

I don't really understand the explanation offered by the book.
C++ has a "one definition rule" (often referred to as the "ODR").

In practical terms the ODR is essentially that the linker should only be
presented with /one/ global definition of something -- whatever it is
-- unless that something is

* an 'inline' function, or

* a templated thing,

in which case the linker will simply choose one of the definitions it
encounters, and blithely assume that all others are exactly the same.

The ODR also holds within a translation unit, but (1) that's not a
problem that occurs very often in practice, and (2) within a translation
unit an ODR violation such as

void foo() {} // 1st definition
void foo() {} // 2nd definition, doesn't matter that it's the same

will be detected by the compiler.

Now, when you for example define a not-'inline' function foo in a header
file, and include that header file in more than one translation unit,
then each translation unit will generate one definition of foo, and the
linker will be presented with multiple global definitions of foo, in
violation of the ODR, and most linkers will then complain.

Here I've used the word "global" for definitions that are accessible and
used throughout the program, as opposed to accessible just locally
within a translation unit. The C++ terms are, respectively, "extern
linkage" for global access, and "internal linkage" for access restricted
to the relevant translation unit. 'const' objects that are declared
outside functions and classes have internal linkage by default, so the
ODR does not apply except if you define the same one more than once in
the translation unit's own code. Therefore, you can freely define
'const' objects in header files, as a practical matter. However,
non-'const' objects that are declared outside functions and classes have
external linkage by default, so the ODR does apply. Include that header
file in more than translation unit and the linker will be unhappy.

--
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?
Aug 8 '06 #4

Alf P. Steinbach wrote:
* Ru*******@gmail.com:
[top-posting]

Please don't top-post in this group. See the FAQ. Corrected.
* Ru*******@gmail.com:
[snip]

I don't really understand the explanation offered by the book.

C++ has a "one definition rule" (often referred to as the "ODR").

In practical terms the ODR is essentially that the linker should only be
presented with /one/ global definition of something -- whatever it is
-- unless that something is

* an 'inline' function, or

* a templated thing,

in which case the linker will simply choose one of the definitions it
encounters, and blithely assume that all others are exactly the same.

The ODR also holds within a translation unit, but (1) that's not a
problem that occurs very often in practice, and (2) within a translation
unit an ODR violation such as

void foo() {} // 1st definition
void foo() {} // 2nd definition, doesn't matter that it's the same

will be detected by the compiler.

Now, when you for example define a not-'inline' function foo in a header
file, and include that header file in more than one translation unit,
then each translation unit will generate one definition of foo, and the
linker will be presented with multiple global definitions of foo, in
violation of the ODR, and most linkers will then complain.

Here I've used the word "global" for definitions that are accessible and
used throughout the program, as opposed to accessible just locally
within a translation unit. The C++ terms are, respectively, "extern
linkage" for global access, and "internal linkage" for access restricted
to the relevant translation unit. 'const' objects that are declared
outside functions and classes have internal linkage by default, so the
ODR does not apply except if you define the same one more than once in
the translation unit's own code. Therefore, you can freely define
'const' objects in header files, as a practical matter. However,
non-'const' objects that are declared outside functions and classes have
external linkage by default, so the ODR does apply. Include that header
file in more than translation unit and the linker will be unhappy.
Thanks a lot for the explanation


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

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

Similar topics

9
by: qazmlp | last post by:
const has internal linkage in C++, but external linkage in C. Am I right ? But, linker reports multiply-defined error if the following header is included in multiple .cpp files. //...
15
by: Dave | last post by:
Hello NG, It is well known that memory-allocating definitions should not be put in a header file. I believe, however, that this does not apply to const definitions. For example: #ifndef...
10
by: S.Tobias | last post by:
1. If I have a struct type which contains a const member: struct mystruct { const int id; int mutable; }; does a definition struct mystruct ms; define an object `ms' which is *partly* const? ...
16
by: recover | last post by:
#include <string> #include <iostream> using namespace std; class TConst { private: string con; string uncon; public:
2
by: Adrian | last post by:
Hi, In a header file I tried const char *someval="this is a test"; which is included all over the place and I get linker errors about multiple defines. Why is this not folded when const char...
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_); }
29
by: Rohit kumar Chandel | last post by:
Hi all, I have a doubt in const keyword. My understanding says that a variable declared const can not be modified by the module it is defined in. Then consider the following code segment:...
10
by: Stephen Howe | last post by:
Hi Just going over some grey areas in my knowledge in C++: 1) If I have const int SomeConst = 1; in a header file, it is global, and it is included in multiple translations units, but it...
5
by: coolguyaroundyou | last post by:
Consider the following codes: class abc { int i; public: abc() : i(0) {} void func() { .....some code....} };
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...

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.