C++ 的 template 測試 2
C++ 的 template 測試 2
#include <iostream>
using namespace std;
class Point1
{
public:
Point1(int x, int y) { mx = x; my = y; }
public:
int mx;
int my;
};
struct Point2
{
public:
Point2(int x, int y) { mx = x; my = y; }
public:
int mx;
int my;
};
template<class T1, class T2>
class Point3
{
public:
Point3(T1 x, T2 y) { mx = x; my = y; }
public:
T1 mx;
T2 my;
};
template<class T1, class T2>
struct Point4
{
public:
Point4(T1 x, T2 y) { mx = x; my = y; }
public:
T1 mx;
T2 my;
};
int main()
{
Point1 *pPoint1 = new Point1(10, 20);
cout << pPoint1->mx << " " << pPoint1->my << endl;
delete pPoint1;
Point2* pPoint2 = new Point2(100, 200);
cout << pPoint2->mx << " " << pPoint2->my << endl;
delete pPoint2;
Point3<int, float>* pPoint3 = new Point3<int, float>(1000, 200.5);
cout << pPoint3->mx << " " << pPoint3->my << endl;
delete pPoint3;
Point3<int, const char *>* pPoint3_1 = new Point3<int, const char* >(1000, "abc");
cout << pPoint3_1->mx << " " << pPoint3_1->my << endl;
delete pPoint3_1;
Point4<int, float>* pPoint4 = new Point4<int, float>(10000, 2000.5);
cout << pPoint4->mx << " " << pPoint4->my << endl;
delete pPoint4;
system("pause");
return 0;
}
留言
張貼留言