先上笔记内容吧:
这次上课的内容有关
- 构造函数
- 析构函数
- 运算符重载
- return * this
内容很细,大家好好回顾笔记再照应程序复习吧 :)
#include <iostream> using namespace std; class Integer {
public:
int i;
int geti () const {return this->i;}
void seti (int i) {this->i = i;}
Integer(int j = );
Integer(Integer & c);
~Integer();
}; Integer::Integer (Integer & c) { //Constructer Function
i = ;
cout << "Integer (Integer & c)" << endl;
} Integer::Integer (int j) : i(j) { //Copy Constructer Function
cout << "Integer (int j)" << endl;
} Integer::~Integer () { //Destructer Function
cout << "~Integer () " << endl;
} const Integer & fun (const Integer & in) {
cout << in.geti () << endl;
return in;
} int main (){ Integer b();
fun(b).geti();
Integer a(b); return ;
}
下面这个程序是有关运算符重载
通过 log print 可以跟踪观察到析构的顺序
同时还有引用返回、友元函数的例子
#include <iostream> using namespace std; class Integer {
private:
int i;
public:
int geti () const {return this->i;}
void seti (int i) {this->i = i;}
Integer(int j = );
Integer(Integer & c);
~Integer(); const Integer & operator = (const Integer & in);
friend const Integer operator + (const Integer & lhs, const Integer & rhs);
}; const Integer & Integer::operator = (const Integer & in) { //Member Function
i = in.i + ;
//return in;
return *this;
} const Integer operator + (const Integer & lhs, const Integer & rhs) { //全局函数
Integer out;
out.i = lhs.i + rhs.i;
return out;
} Integer::Integer (Integer & c) { //Constructer Function
i = ;
cout << "Integer (Integer & c)" << endl;
} Integer::Integer (int j) : i(j) { //Copy Constructer Function
cout << "Integer (int j)" << ' ' << j << endl;
} Integer::~Integer () { //Destructer Function
cout << "~Integer () " << ' ' << i << endl;
} int main (){ Integer b(), a, c, d;
cout << "a.geti()" << a.geti() << endl;
d = c = a = b;
cout << "a.geti()" << a.geti() << endl;
cout << "c.geti()" << c.geti() << endl;
cout << "d.geti()" << d.geti() << endl; d = a + b;
cout << "d.geti()" << d.geti() << endl; return ;
}
接下来运算符重载作一些附加说明
- =
- &
- ,
这三个运算符可以直接使用
运算符重载的时候,如果既有 成员函数,又有全局函数, 优先(?)
eg.
++i (前加) Integer & operator ++ ()
i++ (后加) Integer & operator ++ (int)
括号中的 int 仅仅表示后加,无其他含义
下面的这个例子可以帮助大家更好的理解
#include <iostream> using namespace std; class Integer {
private:
int i;
public:
int geti () const {return this->i;}
void seti (int i) {this->i = i;}
Integer(int j = 0);
Integer(Integer & c);
~Integer(); Integer & operator ++ ();
Integer operator ++ (int);
const Integer & operator = (const Integer & in);
friend const Integer operator + (const Integer & lhs, const Integer & rhs);
}; Integer & Integer::operator ++ () {
++i;
return *this;
} Integer Integer::operator ++ (int) {
Integer temp (this->i);
++temp.i;
return temp;
} const Integer & Integer::operator = (const Integer & in) { //Member Function
i = in.i;
//return in;
return *this;
} const Integer operator + (const Integer & lhs, const Integer & rhs) { //全局函数
Integer out;
out.i = lhs.i + rhs.i;
return out;
} Integer::Integer (Integer & c) { //Constructer Function
i = 18;
cout << "Integer (Integer & c)" << endl;
} Integer::Integer (int j) : i(j) { //Copy Constructer Function
cout << "Integer (int j)" << ' ' << j << endl;
} Integer::~Integer () { //Destructer Function
cout << "~Integer () " << ' ' << i << endl;
} int main (){ Integer b(8), a;
cout << b.geti () << "," << a.geti () << endl;
a = ++b;
cout << b.geti () << "," << a.geti () << endl;
a = b++;
cout << b.geti () << "," << a.geti () << endl; return 0;
}