On 1/24/2016 2:13 PM, Paul wrote:
>
> Why does the below compile? Hasn't the private member ex been
> accessed from outside the class?
>
> class test
> {
> static int ex;
> };
>
> int test::ex = 1;
>
> int main()
> {
>
> }
>
The declaration outside the class is a definition. Of a private member.
It would be rather impractical if you couldn't define private members
outside the class, so the language allows it.
By the way, the declaration is a definition /because/ it's outside the
class, not because of the `=` initialization.
Indeed, because `int` is an integral type, if it were `const` then you
could write
class test
{
static const int ex = 1;
};
const int test::ex;
and still the declaration in the class (now with initialization) would
be a pure declaration, and the declaration after the class a definition.
Cheers & hth.,
- Alf