#2
rjsp2017-01-22 08:27
|
程序代码:
#include<iostream>
using namespace std;
template<class T>
struct Bestandteil
{
Bestandteil(const T& t) :t_(t){}
Bestandteil& operator=(const Bestandteil& b)
{
cout << "Zuweisung von " << b.t_ << "ersetzt" << t_ << endl;
t_ = b.t_;
return *this;
}
T t_;
};
class Test
{
public:
Test(int i, double d, char* c) :i_(i), d_(d), c_(c){}
virtual void ausgeben(ostream& os)const
{
os << i_.t_ << '\t' << d_.t_ << '\t' << c_.t_;
}
private:
Bestandteil<int> i_;
Bestandteil<double> d_;
Bestandteil<char*> c_;
};
void main()
{
Test t1(1, 22.0 / 7, "Halli"),
t2(2, 2.0 * 22 / 7, "Hallo");
cout << t1 << endl << t2 << endl; //此处第一个输出运算符报错
t1 = t2;
cout <<t1 << endl << t2 << endl;
}
using namespace std;
template<class T>
struct Bestandteil
{
Bestandteil(const T& t) :t_(t){}
Bestandteil& operator=(const Bestandteil& b)
{
cout << "Zuweisung von " << b.t_ << "ersetzt" << t_ << endl;
t_ = b.t_;
return *this;
}
T t_;
};
class Test
{
public:
Test(int i, double d, char* c) :i_(i), d_(d), c_(c){}
virtual void ausgeben(ostream& os)const
{
os << i_.t_ << '\t' << d_.t_ << '\t' << c_.t_;
}
private:
Bestandteil<int> i_;
Bestandteil<double> d_;
Bestandteil<char*> c_;
};
void main()
{
Test t1(1, 22.0 / 7, "Halli"),
t2(2, 2.0 * 22 / 7, "Hallo");
cout << t1 << endl << t2 << endl; //此处第一个输出运算符报错
t1 = t2;
cout <<t1 << endl << t2 << endl;
}