C++ でアダプター クラスを作成したいのですが、適応させたいインターフェイスに非仮想メソッドがいくつかあります。通常のアダプタ パターンを引き続き使用できますか?
#include <iostream>
using namespace std;
class NewInterface{
public:
int methodA(){ cout << "A\n"; }
virtual int methodB(){ cout << "B\n"; }
};
class OldInterface{
public:
int methodC(){ cout << "C\n"; }
int methodD(){ cout << "D\n"; }
};
class Old2NewAdapter: public NewInterface {
public:
Old2NewAdapter( OldInterface* a ){ adaptee = a; }
int methodA(){ return adaptee->methodC(); }
int methodB(){ return adaptee->methodD(); }
private:
OldInterface* adaptee;
};
int main( int argc, char** argv )
{
NewInterface* NI = new Old2NewAdapter( new OldInterface() );
NI->methodA();
NI->methodB();
return 0;
}
この設定を行った場合、出力は「C D」ではなく「A D」になります。
では、すべてのメソッドが仮想になるように NewInterface を書き換えずに、OldInterface を NewInterface に適応させるにはどうすればよいでしょうか。