約5年前にC#に切り替えて以来、C++でハードコア開発を行っていません。私はC#でのインターフェイスの使用に精通しており、常にそれらを使用しています。例えば
public interface IMyInterface
{
string SomeString { get; set; }
}
public class MyClass : IMyInterface
{
public string SomeString { get; set; }
}
// This procedure is designed to operate off an interface, not a class.
void SomeProcedure(IMyInterface Param)
{
}
多くの同様のクラスを実装してそれらを渡すことができ、実際に異なるクラスを使用していることを賢くする人はいないので、これはすべて素晴らしいことです。ただし、C ++では、すべてのメソッドが定義されていないクラスをインスタンス化しようとするとコンパイルエラーが発生するため、インターフェイスを渡すことはできません。
class IMyInterface
{
public:
...
// This pure virtual function makes this class abstract.
virtual void IMyInterface::PureVirtualFunction() = 0;
...
}
class MyClass : public IMyInterface
{
public:
...
void IMyInterface::PureVirtualFunction();
...
}
// The problem with this is that you can't declare a function like this in
// C++ since IMyInterface is not instantiateable.
void SomeProcedure(IMyInterface Param)
{
}
では、C ++でC#スタイルのインターフェイスの感触をつかむための適切な方法は何ですか?