摘要: #include using namespace std; class Vector { private: int *dwp; int size; void clone(const Vector v) { this->size=v.size; this->dwp=new int[size]; for(int i=0;idwp[i]=v.dwp[i] } } void dispose() { delete[] dwp; // 删除指针指向的空间,如果不删除,在调用析构函数的时候只会释放指针本身所占用的内存。 } public: //拷贝构造, Vec... 阅读全文
posted @ 2013-08-01 19:04 shouqiang Wei 阅读(180) 评论(0) 推荐(0) 编辑
摘要: #include using namespace std; class Demo { private: int dwint; public: Demo(int i=0):dwint(i) { coutdwint=a.dwint; cout<<"assignment operator used"<<endl; return *this; } }; Demo func(Demo d) { Demo demo; demo=d; return demo; } int main() { Demo a(4); { Demo b; b=func(a); } Dem 阅读全文
posted @ 2013-08-01 19:03 shouqiang Wei 阅读(222) 评论(0) 推荐(0) 编辑
摘要: 当构造函数只有一个参数(或者有多个参数,但是其他参数都带有默认值,(有默认值的参数靠右排放),这样就可以在调用的时候只传一个参数,)就可以被用来做类型转换,#include #include using namespace std; class String { int len ; char * rep; public: String():len(0) { rep=new char[1]; strcpy(rep,""); } String(char* s) { if(!s) { len=0; rep=new char[1]; strcpy(re... 阅读全文
posted @ 2013-08-01 18:57 shouqiang Wei 阅读(340) 评论(0) 推荐(0) 编辑
摘要: ++运算符重载有前置和后置两种,所以要定义一个参数 int来区分前置和后置,带int参数的代表后置,然后再后置函数里面调用前置函数,这样的话就保持了和前置函数一样的自增幅度。在前置函数里面要返回对象的引用,在后置函数里面返回一个对象的拷贝,而对对象进行自增。//单目运算符重载 ++ #include using namespace std; class Fraction { private: int den; int num; public: Fraction(int d=1,int n=0):den(d),num(n) { } //前置 prefix ... 阅读全文
posted @ 2013-08-01 18:51 shouqiang Wei 阅读(265) 评论(0) 推荐(0) 编辑
摘要: == 运算符重载既不能是成员函数也不能是友元函数,只能是普通函数。#include using namespace std; class Vector { public: Vector(int s,int an_array[]); ~Vector() { delete []rep; } int get_size()const { return size; } //重载等号= const Vector& operator=(const Vector & x); int operator[] (int index) { retur... 阅读全文
posted @ 2013-08-01 17:37 shouqiang Wei 阅读(340) 评论(0) 推荐(0) 编辑