64,648
社区成员
发帖
与我相关
我的任务
分享
struct A{
double a1;
double a2;
A operator+(A a);
A operator-(A a);
A operator*(A a);
A operator/(A a);
};
struct B{
double b1;
double b2;
double b3;
B operator+(B b);
B operator-(B b);
B operator*(B b);
B operator/(B b);
};
struct C{
double c1;
double c2;
double c3;
double c4;
C operator+(C c);
C operator-(C c);
C operator*(C c);
C operator/(C c);
};
#include "stdafx.h"
template<class T2> class S {
public:
template<typename C> C operator+ (C& lhs)
{
C temp;
int k = sizeof(T2);
int n = sizeof(lhs) / k;
for (int i = 0; i < n; i++)
{
T2 a = *(T2*)(int(this) + k*i);
T2 b = *(T2*)(int(&lhs) + k*i);
*(T2*)(int(&temp) + i*k) = a + b;
}
return temp;
}
};
template<class T2> class A :public S<T2> {
double a1 = 1;
double a2 = 3;
};
template<class T2> struct B :public S<T2> {
double b1 = 1;
double b2=3;
double b3=5;
};
template<class T2> struct C :public S<T2> {
double c1=5;
double c2=6;
double c3=7;
double c4=8;
};
int main()
{
S<double> a1;
S<double> b1;
S<double> c1 = a1 + b1;
A<double> a2;
A<double> b2;
A<double> c2= a2 + b2;
B<double> a3;
B<double> b3;
B<double> c3 = a3 + b3;
C<double> a4;
C<double> b4;
C<double> c4 = a4 + b4;
return 0;
}