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

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -