c++ - Two objects in namespace to be default initialized by different functions and used by class within namespace -
i have strange request. have namespace foo 2 declared objects of type bar
, namely bar1
, bar2
, initialize via constructor of class bar. sound way so:
namespace foo { class bar; foo::bar *bar1; foo::bar *bar2; } class foo::bar { /* initialize bar1 , bar2 in constructor. */ initializebar(); };
the reason behind shenanigan create object similar cout
, cin
such foo::bar1
, foo::bar2
need not defined user. more specifically, using ncurses
, wish replace cout
output window bar1
, cin
input window bar2
, overload operators , such foo::bar1 << prints
in output window see fit , foo::bar2 >> b
extracts values input window , dumps b. able via c functions call need extend c++. perhaps default initialization of bar1
, bar2
accordingly?
one way use simple function pattern. keep constructor private , clients can access these objects via 2 functions. want disable copy constructor , copy assignment operator well.
// .h namespace foo { class bar: boost noncopyable { public: static bar* getinputwindow() { static bar s_input; return &s_input; } static bar* getoutputwindow() { static bar s_output; return &s_output; } private: bar() { } }; // global/namespace objects clients should directly use extern bar *input; extern bar *output; } // .cpp namespace foo { bar *input = bar::getinputwindow(); bar *output = bar::getoutputwindow(); }
Comments
Post a Comment