C++ declaring a static object in a class -
i'm trying declare static object of class wrote in different class b, this:
class // example { int x; public: a(){ x = 4; } int getx() { return x; } }; class b { static obj1; // <- problem happens here public: static void start(); }; int main() { b::start(); } void b::start() { int x = obj1.getx(); }
what want achieve int x
in b::start()
equal int x
in class a
(4).
i tried googling past hour , understood c++ doesn't allow static objects' declarations. correct?
if so, here's question. how can same result? available workarounds? keeping in mind rest of code depends on functions in class b static.
error
error lnk2001: unresolved external symbol "private: static class b::obj1"
thanks!
you should initialize static var
, code:
class // example { int x; public: a(){ x = 4; } int getx() { return x; } }; class b { static obj1; // <- problem happens here public: static void start(); }; b::obj1; // init static var int main() { b::start(); } void b::start() { int x = obj1.getx(); }