Charles said:
Can someone explain to me what the following means?
"C permits multiple definitions of a variable in any given namespace,
provided the definitions are the same and it generates only a single
variable for the multiple definitions. C++, however, does not permit
redefinition of a variable or any other entity for a very definite
reason that we will discuss later." Chapter 1. C++ Tutorial.
Coronado Enterprises
It means your tutorial is wrong, though I'll admit that the reason it's
wrong is fairly subtle. In fact, it's possible that in attempting to
keep this simple, I'll distort things a bit as well, but I'm pretty
sure that it's still a lot more accurate than what you've quoted above.
Neither C nor C++ allows a single variable to be defined more than once
-- but C has the concept of a "tentative definition" which is absent
from C++. In C, you can have a series of tentative definitions that
result in defining a variable. For example:
int a;
/* ... */
int a = 2;
The first is read as a tenative definition and the second is allowed.
There are limitations on what can be part of a tentative definition
though, so if you have two definitions, neither of which can be
considered a tenative definition, then it's an error. For example:
int a = 2;
/* ... */
int a = 2;
would be an error -- a tentative definition canNOT include
initialization. We're left with two definitions of the same variable,
which, contrary to Coronado's claim, neither C nor C++ allows (even
though the two definitions are identical).
C++ has basically the same rule except that there's no such thing as a
tentative definition -- in the first example above, the 'int a;' will
be treated as the full definition of 'a', so the 'int a = 2;' will not
be allowed. As you probably expect, the second example is rejected by
C++ as well.