代码:

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
#include <iostream>
using namespace std;

template<class _Ty>
class object
{
public:
_Ty _val;
public:
object(_Ty val = 0) : _val(val)
{}
template<class other>
object& operator=(const object<other>& src)
{
if (this != (object<_Ty>*) (&src))
{
this->_val = (_Ty)src._val;
}
return *this;
}
};

int main()
{
object<int> obja(10);
object<double> objb(20);
obja = objb;
cout << obja._val << endl;
return 0;
}

结果:
在这里插入图片描述