Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
typo fixes and code highlighting
Source Link
user4581301
  • 34.1k
  • 7
  • 42
  • 65

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)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 aa.

However, I thought of initialisinginitializing object aa as static A a(0);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 initialiseinitialize it as they had given it in book.

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 initialising 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 initialise it as they had given it in book.

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.

Source Link

Defining a static member

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 initialising 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 initialise it as they had given it in book.