1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
#include <iostream>
#include <memory>
class Interface
{
protected:
virtual Interface* getIndependentCopyImpl() const
{
std::cout << "interface" << std::endl;
return new Interface(*this);
}
public:
std::shared_ptr<Interface> getIndependentCopy() const
{
std::cout << "interface" << std::endl;
return std::shared_ptr<Interface>(getIndependentCopyImpl());
}
};
class Implementation : public Interface
{
protected:
virtual Implementation* getIndependentCopyImpl() const
{
std::cout << "implementation" << std::endl;
return new Implementation(*this);
}
public:
std::shared_ptr<Implementation> getIndependentCopy() const
{
std::cout << "interface" << std::endl;
return std::shared_ptr<Implementation>(getIndependentCopyImpl());
}
};
int main()
{
Interface* i = new Implementation();
std::shared_ptr<Interface> ptr_i = i->getIndependentCopy();
Implementation* i2 = new Implementation();
std::shared_ptr<Implementation> ptr_i2 = i2->getIndependentCopy();
} |
Partager