from : https://cppreference.com/cpp/language/definition

i don't understand what they mean by

"Declaration of a non-inline(since C++17) static data member inside a class definition:"

looking at the code there are definitions and declarations but i tried switching between the drafts and saw no difference

can someone please help understand this what are they trying to say ? i am new to c++ and english is not my first language

struct S
{
    int n;               // defines S::n
    static int i;        // declares, but does not define S::i
    inline static int x; // defines S::x
};                       // defines S

int S::i;                // defines S::i

Recommended Answers

All 2 Replies

  1. This is the incorrect use of the post type "code snippet." Read what it is, how and when to use that.

  2. I'd use "Order-based initialization"
    Example that might replace line 8.

    // Order-based initialization
    Player s{1, 2, 3};

Now you would have an object Player with the initial values you wanted.

from : https://cppreference.com/cpp/language/definition

i don't understand what they mean by

"Declaration of a non-inline(since C++17) static data member inside a class definition:"

looking at the code there are definitions and declarations but i tried switching between the drafts and saw no difference

can someone please help understand this what are they trying to say ? i am new to c++ and english is not my first language

Classes and structures do not create any data storage until an instance if them is declared (e.g. S s1, s2, s3). At that time, non-static data members come into existence.
A static class/struct member is common to all instances (s1, s2 and s3 all share the same value of i so if you change s1.i s2 and s3 will also have the changed value.
Static data have storage and initial value (default 0 but good practice to explicitly give a value) allocated at compile time before and instances are created. You therefore need to provide a definition somewhere S::i so that this happens.
By saying ‘inline’ the compiler knows you are not going to do this and it creates space for i there and then.
Others may be able to explain more clearly but I hope that helps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.