0

If I will define a private static field in class. Given that it's a private field, can't I initialize it outside the class?

class X{
    static int private_static_field;
public:
    static void setPrivateStaticField(int val);
};

void X::setPrivateStaticField(int val) {
    private_static_field = val;
}


int X::private_static_field(0); // something like that it's ok? if yes, I must write this line? why? can I write it in main?

It's look that it's ok (according to the compiler), but if so, I don't understand the concept of private - How it's ok if it's outside the class?


In addition, given the class above, and the next code:

int main() {
    X x1();
    x1.setPrivateStaticField(3);
    return 0;
}

What is the meaning of x1.setPrivateStaticField(3); , after all, this function is static and hence it's not related to some object.
Hence, I don't understand how it's ok to call setPrivateStaticField with object (x1) ?
(I thought that just X::setPrivateStaticField(3); will be ok and that x1.setPrivateStaticField(3); will be error)

Software_t
  • 576
  • 1
  • 4
  • 13
  • Short answer: Because those are the rules. – IInspectable Mar 13 '18 at 11:11
  • 1
    `int X::private_static_field(0);` is a definition of static field and it works because it is a part of class definition. Since variable is private you can not write `X::private_static_field` anywhere outside of class itself or its friends. Second question should've been a separate question, and the answer would be to allow making member functions static / non-static without requiring to rewrite all the calls. – user7860670 Mar 13 '18 at 11:12

2 Answers2

1

Within a class definition static data members are declared but not defined. So they even can have an incomplete type.

So this record

int X::private_static_field(0);

is a definition of the static data member declared in the class definition like

class X{
    static int private_static_field;
    // ...

This record

x1.setPrivateStaticField(3);

means an access to a class member. Static members are also class members. Using this access method the compiler will know to search the name setPrivateStaticField in the class definition because the name x1 defines an object of the class X.

Another way to access a static member is to use the following record

X::setPrivateStaticField
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

I don't understand the concept of private - How it's ok if it's outside the class?

There is no contradiction here. Prior to C++ 17 static member variables required a definition that is placed separately from the class declaration.

Despite the fact that the text of the definition is placed outside the class, the member variable remains part of the class where it is declared, and retains its accessibility according to its declaration inside the class. This includes private accessibility.

What is the meaning of x1.setPrivateStaticField(3); , after all, this function is static and hence it's not related to some object.

Although C++ compiler lets you call static member functions on the object, it is cleaner to call them using scope resolution operator :: and the class name:

X::setPrivateStaticField(3);

Allowing or disallowing class method calls on an instance is up to the designers of the language. C++ designers decided to allow it; designers of other programming languages disallow it, or require compilers to issue a warning.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523