c++ - Adding a collection of functions to an object at runtime -
what id implement roles programming technique within code. using c++. c++11 fine.
what need able define collection of functions. collection can not have state.
some of functions collection has deferred/delegated.
e.g. (illustration only:)
class account { int balance = 100; void withdraw(int amount) { balance -= amount; } } account savings_account; class sourceaccount { void withdraw(int amount); // deferred. void deposit_wages() { this->withdraw(10); } void change_pin() { this->deposit_wages(); } } sourceaccount *s; s = savings_account; // s savings_account obj, // can call sourceaccount methods. s->withdraw(...); s->deposit(); s->change_pin();  i dont want include sourceaccount base class of account , cast, want simulate runtime inheritance. (account doesnt know sourceaccount)
im open suggestions; can extern or similar function inside sourceaccount class? c++11 unions? c++11 call forwarding? changing 'this' pointer?
thankyou
it sounds want create sourceaccount (or variety of other classes) takes reference account , have methods of enclosing class delegate account:
class sourceaccount{ account& account; public: explicit sourceaccount(account& a):account(a){} void withdraw(int amount){ account.withdraw(amount); } // other methods can either call methods of class // or delegate account };  
Comments
Post a Comment