Featured post
Run specific code when overloading C++ operators -
i have class, let's call foo, contains 3 following methods (overloading left-associative < binary operator):
... operator<(a a) { return *this; } ... operator<(b b) { return *this; } ... operator<(c c) { return *this; }
a, b, c classes not related in way(if that, of matter).
now in program have 2 following cases:
a = new a(); b b = new b(); c c = new c(); (first case): new foo() < < b;
or
(second case): new foo() < < b < c;
whenever have first case (which ends b
), want execute function run() when have read(i know) b instance. idea have following code in foo class:
... operator<(b b) { run(); }
now when have code of first case, run()
being executed.
the problem when have code in second case(which ends in c
). want execute run()
function again not until know c
is. if have previous piece of code run()
called when doing < b
not want don't know c
yet. if add run()
in operator<(c c)
call run()
twice.
in few words want accomplish when having first case call run()
@ operator<(b b)
, when have second case call run @ operator<(c c)
.
any ideas on how can solved(if can)?
class prefoo { public: prefoo() { ... } prefoo & operator<<(a a) { ...; return *this; } prefoo & operator<<(b b) { ...; return *this; } prefoo & operator<<(c c) { ...; return *this; } int aa, bb, cc; // save information }; prefoo makefoo() { return prefoo(); } class foo { public: foo(const prefoo & pre) { run(); } // implicit conversion void run() {} // run!! }; void g() { a; b b; c c; // implicit conversion @ end foo foo1 = makefoo() << << b; foo foo2 = prefoo() << << b << c; }
- Get link
- X
- Other Apps
Comments
Post a Comment