-3

I thought that we can redeclare a name any times in any context. But

class A 
{
    static int a;
    static int a;
};

returns a compile-time error:

test.cpp:4:13: error: redeclaration of ‘int A::a’
test.cpp:3:13: note: previous declaration ‘int A::a’

What names can be redeclare actually?

St.Antario
  • 26,175
  • 41
  • 130
  • 318

3 Answers3

2

According to the C++ Standard (9.2 Class members, paragraph #1)

A member shall not be declared twice in the member-specification, except that a nested class or member class template can be declared and then later defined, and except that an enumeration can be introduced with an opaque-enum-declaration and later redeclared with an enum-specifier.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

C++11 9.2/1 [class.mem]

A member shall not be declared twice in the member-specification, except that a nested class or member class template can be declared and then later defined, and except that an enumeration can be introduced with an opaque-enum-declaration and later redeclared with an enum-specifier.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
0

Beside the other answers, your code is not only a redeclaration but also a redefinition, which clearly violates the one-definition-rule.

bjhend
  • 1,538
  • 11
  • 25
  • There is no any redefinition. Actually, `declaration is a definition unless it declares a function without specifying the function’s body (8.4), it contains the extern specifier (7.1.1) or a linkage-specification 25 (7.5) and neither an initializer nor a function-body, it declares a static data member in a class definition (9.2, 9.4)` – St.Antario May 13 '14 at 07:50
  • For more details please see 3.1.2 section – St.Antario May 13 '14 at 07:50
  • You're right. I've read too quickly and missed that you define a class not a function. – bjhend May 13 '14 at 07:59