Skip to content

Instantly share code, notes, and snippets.

@tanakh
Created March 8, 2010 02:37
Show Gist options
  • Save tanakh/324790 to your computer and use it in GitHub Desktop.
Save tanakh/324790 to your computer and use it in GitHub Desktop.
#include <iostream>
using namespace std;
#include <boost/scoped_ptr.hpp>
using namespace boost;
class clonable{
public:
virtual ~clonable(){}
virtual clonable *clone() const = 0;
};
class A : public virtual clonable{
public:
void foo(){
scoped_ptr<A> p1(dynamic_cast<A*>(this->clone()));
scoped_ptr<A> p2(dynamic_cast<A*>(this->clone()));
p1->dat=123;
p2->dat=456;
cout<<p1->dat<<" "<<p2->dat<<endl;
}
int dat;
};
template <class T>
class cloner : public virtual clonable{
public:
clonable *clone() const {
return new T(dynamic_cast<const T&>(*this));
}
};
class B : public A, cloner<B>{};
int main()
{
B b;
b.foo();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment