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?