1

idがやりたいのは、コード内にロールプログラミング手法を実装することです。私はC++を使用しています。C++11で問題ありません。

私が必要としているのは、関数のコレクションを定義できるようにすることです。このコレクションは状態を持つことができません。

このコレクションが持つ機能の一部は、延期/委任されます。

例(図のみ:)

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();

実行時の継承をシミュレートしたいので、ACCOUNTの基本クラスとしてSOURCEACCOUNTを含めて、キャストを実行したくありません。(ACCOUNTはSOURCEACCOUNTについて知りません)

私はどんな提案にもオープンです。SOURCEACCOUNTクラス内の関数をexternまたは同様のものにすることはできますか?C ++ 11ユニオン?C ++ 11コール転送?'this'ポインタを変更しますか?

ありがとうございました

4

1 に答える 1

0

への参照を取り、それを囲むクラスのいくつかのメソッドを次のように委任するSOURCEACCOUNT(または他のさまざまなクラス)を作成したいようです。ACCOUNTACCOUNT

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
};
于 2012-07-07T11:36:56.160 に答える