Created
March 8, 2010 02:54
-
-
Save tanakh/324796 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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&>(dynamic_cast<const clonable&>(*this))); | |
//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