나는 유사한 인터페이스 클래스가 구체적인 클래스 AImplementation의 인스턴스를 생성합니다. 예 : 그런 다음가장 좋은 방법 ++ 인터페이스
class IInterface
{
public:
virtual ~IInterface() {}
static std::tr1::shared_ptr<IInterface> create(); // implementation in .cpp
virtual methodA() = 0;
virtual methodB() = 0;
};
내가 지금 같은 주에있는 인터페이스를 사용할 수있을 것입니다 :
int main()
{
AImplementation* ai = new AIImplementation();
}
또는 더 나은 다음과 같은 인터페이스의 멤버 함수를 "만드는"공장을 넣어하는 것입니다
int main()
{
std::tr1::shared_ptr<IInterface> test(IInterface::create());
}
첫 번째 옵션은 일반적인 관행 (해당 권한을 말하지 않는 것) 인 것처럼 보입니다. 그러나 두 번째 옵션은 "Effective C++"에서 유래되었습니다.
언어의 정확성에 대한 몇 가지 메모가 있습니다. C++에서는 메소드가없고 멤버 함수가 있습니다. – Artyom
다음과 같이 main()을 작성합니다. 'int main() { IInterface * i = new AIImplementation(); }' –