代码:
#include<iostream> using namespace std; template <typename T> struct Point{ T x,y; Point(T x=0,T y=0)/*:x(x),y(y)*/{ this->x=x; this->y=y; } }; template<typename T> Point<T> operator +(const Point<T> &A,const Point<T> &B){ return Point<T>(A.x+B.x,A.y+B.y); } template<typename T> ostream&operator<<(ostream &out,const Point<T> &p){ out<<"("<<p.x<<","<<p.y<<")"; } template<typename T>T sum(T *begin,T *end){ T ans=0; for(T *p=begin;p!=end;p++){ ans=ans+*p; } return ans; } int main(){ Point<int> a; Point<int> b(1,2); a.x=3; Point<int> c=a+b; cout<<c<<endl; double m[]={1.1,2.2,3.3,4.4}; cout<<sum(m,m+4)<<endl; Point<double> n[]={Point<double>(1.1,2.2),Point<double>(3.3,4.4),Point<double>(5.5,6.6),Point<double>(7.7,8.8)}; cout<<sum(n,n+4)<<endl; return 0; }