Question

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?

Was it helpful?

Solution 2

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.

OTHER TIPS

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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top