c++ - Call a function while setting value of a declarement -
i want call function while setting value. example:
int i; = 123; //here want call function. //want this: //i = 123;func(); //but not want this.
can add new operator can this?
int i; $ 123; //set = 123 , call function.
if need trigger function call on assignment, can wrap type in class overrides assignment operator; note may want override more assignment, example , not style guide :)
#include <iostream> template<class t> class wrap { private: t value; void (*fn)(); public: wrap(void (*_fn)()) { fn=_fn; } t& operator=(const t& in) { value = in; fn(); return value;} operator t() { return value; } }; void func() { std::cout << "func() called!" << std::endl; } int main(void) { wrap<int> i(func); i=5; // assigns , calls func() std::cout << << std::endl; // still usable int } > output: > func() called! > 5
Comments
Post a Comment