Why re-declaration is legal in C++?

Discussion in 'C++' started by Universe, Mar 25, 2011.

  1. Universe

    Universe Guest

    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?
    Universe, Mar 25, 2011
    #1
    1. Advertising

  2. On 25 mar, 10:32, Universe <> wrote:.
    > 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).

    --
    Michael
    Michael Doubez, Mar 25, 2011
    #2
    1. Advertising

  3. Universe

    Öö Tiib Guest

    On Mar 25, 11:32 am, Universe <> wrote:
    > 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, Mar 25, 2011
    #3
  4. Universe <> wrote:
    > As redefinition is illegal in C++, could someone tell me
    > why re-declaration is legal?


    Why shouldn't it be legal?
    Juha Nieminen, Mar 25, 2011
    #4
  5. Universe

    Öö Tiib Guest

    On Mar 25, 5:36 pm, Juha Nieminen <> wrote:
    > Universe <> wrote:
    > > As redefinition is illegal in C++, could someone tell me
    > > why re-declaration is legal?

    >
    > 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.
    Öö Tiib, Mar 25, 2011
    #5
  6. Universe

    Paul Guest

    "Juha Nieminen" <> wrote in message
    news:4d8cb660$0$2846$...
    > Universe <> wrote:
    >> As redefinition is illegal in C++, could someone tell me
    >> why re-declaration is legal?

    >
    > Why shouldn't it be legal?
    >

    I think it would make the compiler-programmers' job impossible,
    Paul, Mar 25, 2011
    #6
  7. Universe

    Universe Guest

    Thank you for answer :)
    Universe, Mar 26, 2011
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joost Kraaijeveld

    Is this forward declaration legal C++?

    Joost Kraaijeveld, Jun 28, 2004, in forum: C++
    Replies:
    7
    Views:
    453
    Sharad Kala
    Jun 29, 2004
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,764
    Smokey Grindel
    Dec 2, 2006
  3. Replies:
    3
    Views:
    437
  4. Carramba

    is this legal declaration/not correct output

    Carramba, May 16, 2007, in forum: C Programming
    Replies:
    12
    Views:
    557
    David Thompson
    Jul 1, 2007
  5. Replies:
    2
    Views:
    346
Loading...

Share This Page