What id like to do is implement the roles programming technique within my code. I am using C++. C++11 is fine.
What i need is to be able to define a collection of functions. This collection can not have state.
Some of the functions that this collection has will be 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 is actually the savings_account obj,
// But i can call SOURCEACCOUNT methods.
s->withdraw(...);
s->deposit();
s->change_pin();
I dont want to include SOURCEACCOUNT as a Base Class of ACCOUNT and do a cast, as i want to simulate runtime inheritance. (ACCOUNT doesnt know about SOURCEACCOUNT)
Im open to any suggestions; Can i extern or similar the function inside the SOURCEACCOUNT class? C++11 unions? C++11 call forwarding? changing the 'this' pointer?
Thankyou
It sounds like you want to create a SOURCEACCOUNT
(or a variety of other classes) which takes a reference to an ACCOUNT
and have some methods of the enclosing class delegate to ACCOUNT
:
class SOURCEACCOUNT{
ACCOUNT& account;
public:
explicit SOURCEACCOUNT(ACCOUNT& a):account(a){}
void withdraw(int amount){ account.withdraw(amount); }
// other methods which can either call methods of this class
// or delegate to account
};