c++ - define class as a function returning a reference -
i trying compile old source code person in lab. , cannot understand idea of following trick. why wants access clog2factory class trough reference given glogfactory()? , how can compile , link code below?
the whole code big, made short example. keep friend , virtual present in original code.
test.h:
class clog2; class clog2factory { friend class clog2; public: clog2factory() {} virtual int init() = 0; }; clog2factory& glogfactory(); class clog2 { public: clog2() : glogfactory().init() { } };
test.cpp:
#include "test.h" int main(int argc, char *argv[]) { clog2 log; return 0; }
i following error:
test.h: in constructor ‘clog2::clog2()’: test.h:50:15: error: class ‘clog2’ not have field named ‘glogfactory’ clog2() : glogfactory().init() ^ test.h:50:28: error: expected ‘{’ before ‘.’ token clog2() : glogfactory().init()
in fact, compile original code (perhaps missing in test example). couldn't link it. error linking of original code following(file test.h changed log2.h now):
log2.h:254: undefined reference `glogfactory()'
your code missing original : a member (which type returned init()
method) initialized initialization list, :
class clog2 { public: clog2() : x(glogfactory().init()) { } int x; };
Comments
Post a Comment