Last active
December 15, 2015 20:00
-
-
Save asquared/5315243 to your computer and use it in GitHub Desktop.
If you define BREAK_IT, this doesn't compile. I'm not sure why.
This file contains 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; | |
template <typename T> | |
class A; | |
template <typename T> | |
class B; | |
template <typename T> | |
class A { | |
public: | |
T data; | |
A() { data = 0; } | |
A(T data_) { data = data_; } | |
A(const A<T> &src) { data = src.data; } | |
const A<T> &operator=(const A<T> &rhs) { data = rhs.data; return *this; } | |
template <class U> A<U> *copy( ) { return new A<U>(data); } | |
template <class U> B<U> *to_B( ); | |
}; | |
template <typename T> | |
class B { | |
public: | |
T data; | |
B() { data = 0; } | |
B(T data_) { data = data_; } | |
B(const B<T> &src) { data = src.data; } | |
const B<T> &operator=(const B<T> &rhs) { data = rhs.data; return *this; } | |
template <class U> A<U> *to_A( ); | |
template <class U> B<U> *copy( ) { return new B<U>(data); } | |
}; | |
template <typename T> template <typename U> | |
B<U> *A<T>::to_B( ) { | |
return new B<U>(data); | |
} | |
template <typename T> template <typename U> | |
A<U> *B<T>::to_A( ) { | |
return new A<U>(data); | |
} | |
#ifdef BREAK_IT | |
template <typename T> | |
class C { | |
public: | |
C(T data_) { data = data_; } | |
template <typename U> void x(A<U> &a) { | |
a.data = data; | |
} | |
template <typename U> void x(B<U> &b) { | |
A<U> tmp; | |
x(tmp); /* tmp.data = data */ | |
B<U> *tmp2 = tmp.to_B<U>( ); /* tmp2->data = tmp.data */ | |
b = *tmp2; /* b.data = tmp2->data */ | |
delete tmp2; | |
} | |
T data; | |
}; | |
#endif | |
template <typename U> B<U> *copy_b(B<U> &b) { | |
return b.copy<U>( ); | |
} | |
int main( ) { | |
A<float> a(2.0f); | |
B<float> b(2.0f); | |
A<int> *a2 = b.to_A<int>( ); | |
B<int> *b2 = a.to_B<int>( ); | |
B<float> *b3 = copy_b(b); | |
cout << "a.data=" << a.data << endl; | |
cout << "b.data=" << b.data << endl; | |
cout << "a2->data=" << a2->data << endl; | |
cout << "b2->data=" << b2->data << endl; | |
cout << "b3->data=" << b3->data << endl; | |
delete a2; | |
delete b2; | |
delete b3; | |
#ifdef BREAK_IT | |
C<int> c(1); | |
c.x(a); | |
c.x(b); | |
cout << "a.data=" << a.data << endl; | |
cout << "b.data=" << b.data << endl; | |
#endif | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment