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.

4
  • Are you placing static A a(0); inside or outside the class definition? Why it could be wrong changes depending on the location. Commented Jun 29, 2021 at 2: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. Commented Jun 29, 2021 at 2: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? Commented Jun 29, 2021 at 2:57
  • 1
    Possible duplicate: stackoverflow.com/q/68156052/2079303 Commented Jun 29, 2021 at 2:57

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

6 Comments

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 "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.
@loveofprogramming eerorika's answer to a similar question deals with the "why" to some extent.
@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.
Agreed, it could, but if that were easy, the static initialization order fiasco wouldn't be such a fiasco.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.