forward definition of c++ class member functions -
i'm writing small generator outputs c++ classes in single cpp file tree object structure ( order of output of classes given tree, fixed).
to keep simple prefer if there way keep in 1 file.
the problem these classes interact each other using member functions , there case forward declarations arent working.
example:
#include <iostream> using namespace std; class b; b* global_b=null; class a; a* global_a=null; class { public: a() {} ~a() {} void accessb() { global_b->setvalue(1); } int getvalue() { return 2; } }; class b { public: b() : j(0) {} ~b(){} void setvalue(int i) { j = + global_a->getvalue(); } int j; }; int main() { global_b = new b(); global_a = new a(); global_a->accessb(); cout << "hello world!" << endl; return 0; }
any suggestions/ideas? thanks.
typically, you'd this:
class { public: a() {} ~a() {} void accessb(); ... };
then somewhere after both , b declared:
void a::accessb() { global_b->setvalue(1); }
Comments
Post a Comment