1

Consider the following code:

#include <iostream >
using namespace std;

class A
{
private:
    int x;
public:
    A(int _x)  {  x = _x; }
    int get()  { return x; }
};

class B
{
    static A a;
public:
   static int get()
   {  return a.get(); }
};

A B::a(0);

int main(void)
{
    B b;
    cout << b.get();
    return 0;
}

My book says:

If we do not use the line of code A B::a(0),there is a compiler error because static member a is not defined in B. To fix the error, we need to explicitly define a.

However, I thought of initializing object a as static A a(0); but it gives me a compiler error. Can someone explain why I can't initialize object a in the manner I described, and why it is necessary to initialize it as they had given it in book.

user4581301
  • 33,082
  • 7
  • 33
  • 54
  • Are you placing `static A a(0);` inside or outside the class definition? Why it could be wrong changes depending on the location. – user4581301 Jun 29 '21 at 02:48
  • 1
    Side note: what you can get away with in `static` members is something of a moving target. Every new revision of the C++ Standard relaxes some of the restrictions and adds a bit more functionality. – user4581301 Jun 29 '21 at 02:50
  • @user4581301 I am trying to place static A a(0) inside the class definition. Can you please tell why it would be wrong to place it here? – loveofprogramming Jun 29 '21 at 02:57
  • 1
    Possible duplicate: https://stackoverflow.com/q/68156052/2079303 – eerorika Jun 29 '21 at 02:57

1 Answers1

1

If you want to define a inline, you need to inline it, which is possible from C++17:

class B {
    inline static A a{0};    // or   inline static A a = 0;
public:
    static int get() { return a.get(); }
};

Demo

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
  • Buy why do I need to use the keyword inline? Also can I define it as inline static A(0) instead of curly brackets? Also, do other standards of C++ allow this? – loveofprogramming Jun 29 '21 at 02:58
  • @loveofprogramming "_But why_": Basically just because the language requires it. I don't know the reason why. That's just the way it is. No, not without curly brackets. Either curly brackets or `=`. No, the standards before C++17 doesn't allow it. – Ted Lyngmo Jun 29 '21 at 03:00
  • 1
    @loveofprogramming [eerorika](https://stackoverflow.com/a/68156223/7582247)'s answer to a similar question deals with the "_why_" to some extent. – Ted Lyngmo Jun 29 '21 at 03:09
  • A rough generalization is there is one instance of the `static` variable for all instances of the class. Since it's not part of any one class instance, storage had to be allocated outside the class, in one-and-only-one place. If you declared it in the class and that class was in a header and that header was included in many files, which of these many files would contain the one-and-only-one definition? How could you be sure that it was in only one file? – user4581301 Jun 29 '21 at 03:09
  • 1
    @user4581301 The compiler could probably pick the first definition it finds when linking and the standard could just say it's UB to have conflicting definitions. I guess `inline` is required just to make a distinction between the two ways of defining `static` members but I don't know if there would be any drawbacks to just drop the requirement for `inline` in the future now that both member functions and variables can be `inline`. A `static` variable definition could probably be implicitly `inline` just like functions that are defined in class definitions are implicitly `inline`. – Ted Lyngmo Jun 29 '21 at 03:52
  • 1
    Agreed, it could, but if that were easy, the static initialization order fiasco wouldn't be such a fiasco. – user4581301 Jun 29 '21 at 04:12