I have an interface class similar to:
class IInterface
{
public:
virtual ~IInterface() {}
virtual methodA() = 0;
virtual methodB() = 0;
};
Then I implement the interface:
class AImplementation : public IInterface
{
}
When I use the interface in an application, it is better to instantiate a specific AImplementation class. For example.
int main()
{
AImplementation* ai = new AIImplementation();
}
Or it is better to place the factory create function in the interface, as shown below:
class IInterface
{
public:
virtual ~IInterface() {}
static std::tr1::shared_ptr<IInterface> create();
virtual methodA() = 0;
virtual methodB() = 0;
};
Then I could use the interface basically like this:
int main()
{
std::tr1::shared_ptr<IInterface> test(IInterface::create());
}
The first option seems common practice (not to mention his right). However, the second option was obtained from Effective C ++.
source
share