Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

private static member variables

26 views
Skip to first unread message

Paul

unread,
Jan 24, 2016, 8:13:38 AM1/24/16
to

Why does the below compile? Hasn't the private member ex been accessed from outside the class?

Thanks,

Paul

class test
{
static int ex;
};

int test::ex = 1;

int main()
{

}

Alf P. Steinbach

unread,
Jan 24, 2016, 8:22:53 AM1/24/16
to
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

Paavo Helde

unread,
Jan 24, 2016, 8:26:17 AM1/24/16
to
No, the prefix test:: shows the definition belongs into the class test
and in this sense this line is "inside the class".

This is the same as defining member functions outside of the class
definition itself.

hth
Paavo

Paavo Helde

unread,
Jan 24, 2016, 8:56:52 AM1/24/16
to
On 24.01.2016 15:48, Stefan Ram wrote:
> "Alf P. Steinbach" <alf.p.stein...@gmail.com> writes:
>> 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.
>
> In C++, we even deem »operator +« and »operator >>« to be
> »part of the class ::pair::pair«, even though they're not
> even declared within the class specifier.

Who is "we"? And no, these are not part of the class (they cannot access
private members of the class, for example).

Maybe you wanted to say such operators may be considered as a part of
the informal *interface* of the class?



Paavo Helde

unread,
Jan 24, 2016, 9:21:14 AM1/24/16
to
On 24.01.2016 16:02, Stefan Ram wrote:
> Paavo Helde <myfir...@osa.pri.ee> writes:
>> Who is "we"? And no, these are not part of the class (they cannot access
>> private members of the class, for example).
>
> »We« is Herb Sutter who wrote:
>
> »For a class X, all functions, including free functions, that both
> (a) "mention" X, and
> (b) are "supplied with" X
> are logically part of X«
>

Note he uses term "logically", as an opposite to "actually".

Gareth Owen

unread,
Jan 24, 2016, 10:48:54 AM1/24/16
to
Notice what Stefan originally said, with which you dissented:

In C++, we even deem »operator +« and »operator >>« to be
»part of the class ::pair::pair«, even though they're not
even declared within the class specifier.

It's clear that Stefan and Herb's view on this are the same.
"they are logically part of X" <=> "we deem them to be part of X"

Chris Vine

unread,
Jan 24, 2016, 1:44:19 PM1/24/16
to
I think that is a false argument, but whether you are right or wrong,
the response by Stefan was (in classic Stefan style) of no relevance to
the question raised by the original poster.

There is no comparison to be made between private static data members,
and free standing functions which have a class type as their first
argument. The first are not part of the class interface, whereas the
second are part of the class interface in some people's view (including
mine).

Chris

Gareth Owen

unread,
Jan 24, 2016, 2:56:16 PM1/24/16
to
Chris Vine <chris@cvine--nospam--.freeserve.co.uk> writes:
>
> I think that is a false argument, but whether you are right or wrong,
> the response by Stefan was (in classic Stefan style) of no relevance to
> the question raised by the original poster.

You'll get no disagreement. The question is whether Stefan's classic
irrelevance required Paavo's classic pedantry.
0 new messages