Why re-declaration is legal in C++?

U

Universe

Hello, everyone.
I found the same class declaration can be used more than once in the
same namespace.
For example, there is a C++ file which contains:

class A;
class A;

This can be compiled with no errors. I have tested in g++ with Mingw
and VC 2008. As redefinition is illegal in C++, could someone tell me
why re-declaration is legal?
 
M

Michael Doubez

I found the same class declaration can be used more than once in the
same namespace.
For example, there is a C++ file which contains:

class A;
class A;

This can be compiled with no errors. I have tested in g++ with Mingw
and VC 2008. As redefinition is illegal in C++, could someone tell me
why re-declaration is legal?

Otherwise it would be a hell. Imagine the B.h and C.h both declares
class A, you wouldn't be able to include both.

And there is nothing wrong with multiple declaration, they only
introduce a name in the scope.
In fact, formally, there is only one declaration in your case, further
declarations refers to the declared name i.e. it doesn't modify the
point of declaration).
 
Ö

Öö Tiib

Hello, everyone.
I found the same class declaration can be used more than once in the
same namespace.
For example, there is a C++ file which contains:

class A;
class A;

This can be compiled with no errors.

Yes. Additionally you can add typedef (that is done implicitly by
above declaration). Also you may address a class either as 'class' or
as 'struct' (since the keywords are synonymes making difference only
for definition). Compilers might warn but it is legal. So such code
should compile:

class A;
typedef class A A;
struct A;
typedef struct A A;
I have tested in g++ with Mingw
and VC 2008. As redefinition is illegal in C++, could someone tell me
why re-declaration is legal?

Redeclaration (changing a declaration) is illegal (so you can not
declare there is a "enum A" or "union A" or "typedef int A" once you
have declared that A is class ). What you do is not redeclaration. It
is repeating same declaration.

It is needed because of C++ way of building code up by merging various
pieces of texts together with preprocessors #include directives in
such texts. So if you do not know if a class is already declared (or
even defined) by previously included code or not you can declare it.
 
Ö

Öö Tiib

Why shouldn't it be legal?

Because it would be pointless if there was no such mess with that damn
preprocessor. If interface declaration, dependency declaration and
implementation was clearly separated (like for example in Ada) ...
then declaring something twice would indicate low attention and
probable typo.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top