64,282
社区成员
发帖
与我相关
我的任务
分享
#include "stdafx.h"
template<class T2>
class S {
public:
S<T2> operator= (S<T2>& lhs)
{
S<T2> temp(lhs);
int k = sizeof(T2);
int n = sizeof(S<T2>) / k;
for (int i = 0; i < n; i++)
{
T2 a = *(T2*)(int(&lhs) + k);
T2 b = *(T2*)(int(&rhs) + k);
*(T2*)(int(&temp) + k) = a + b;
}
return temp;
}
friend S<T2> operator+ (S<T2>& lhs, S<T2>& rhs)
{
S<T2> temp(lhs);
int k = sizeof(T2);
int n = sizeof(S<T2>) / k;
for (int i = 0; i < n; i++)
{
T2 a = *(T2*)(int(&lhs) + k);
T2 b = *(T2*)(int(&rhs) + k);
*(T2*)(int(&temp) + k) = a + b;
}
return temp;
}
};
template<class T2>
class A :public S<T2>
{
public:
T2 x = 0;
T2 y = 2;
T2 z = 5;
};
int main()
{
S<double> a1;
S<double> b1;
S<double> c1 = a1 + b1;//该行通过
A<double> a;
A<double> b;
A<double> c=a+b;//该行报错
return 0;
}
#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>
{
public:
T2 x = 0;
T2 y = 2;
T2 z = 5;
};
int main()
{
S<double> a1;
S<double> b1;
S<double> c1 = a1 + b1;//该行通过
A<double> a;
A<double> b;
a.x = 2;
a.y = 5;
a.z = 9;
A<double> c= a + b;//该行报错
return 0;
}
#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>
{
public:
T2 x = 0;
T2 y = 2;
T2 z = 5;
};
int main()
{
S<double> a1;
S<double> b1;
S<double> c1 = a1 + b1;//该行通过
A<double> a;
A<double> b;
a.x = 2;
a.y = 5;
a.z = 9;
A<double> c= a + b;//该行报错
return 0;
}