- #include <stdio.h>
- #include <iostream>
- using namespace std;
- //#define TEST_WITH_SHALLOW_COPY
- class student
- {
- private:
- int m_id;
- int m_age;
- char * m_name;
- public:
- //无参数构造函数
- //如果创建一个类你没有写任何构造函数,则系统会自动生成默认的无参构造函数,函数为空,什么都不做
- //只要你写了一个下面的某一种构造函数,系统就不会再自动生成这样一个默认的构造函数,如果希望有一个这样的无参构造函数,则需要自己显示地写出来
- student(void)
- {
- cout << "call no-param constructor function ~" << endl;
- m_id = 0;
- m_age = 0;
- m_name = new char[20];
- strcpy(m_name, "default_name");
- }
- ~student(void)
- {
- cout << "call destructor function ~" << endl;
- m_id = 0;
- m_age = 0;
- if(NULL != m_name)
- {
- delete [] m_name;
- }
- }
- //一般构造函数(也称重载构造函数)
- //一般构造函数可以有各种参数形式,一个类可以有多个一般构造函数,前提是参数的个数或者类型不同(基于c++的重载函数原理)
- // 例如:你还可以写一个 student( int num)的构造函数出来
- // 创建对象时根据传入的参数不同调用不同的构造函数
- student(int real, int imag, char *name)
- {
- cout << "call common(param) constructor function ~" << endl;
- m_id = real;
- m_age = imag;
- m_name = "";
- if(NULL != name)
- {
- m_name = new char[strlen(name)+ 1];
- strcpy(m_name, name);
- }
- }
- //复制构造函数(也称为拷贝构造函数)
- //复制构造函数参数为类对象本身的引用,用于根据一个已存在的对象复制出一个新的该类的对象,一般在函数中会将已存在对象的数据成员的值复制一份到新创建的对象中
- //若没有显示的写复制构造函数,则系统会默认创建一个复制构造函数,但当类中有指针成员时,由系统默认创建该复制构造函数会存在风险,具体原因请查询 有关 “浅拷贝” 、“深拷贝”的文章论述
- student(const student & c)
- {
- cout << "call copy constructor function ~" << endl;
- m_id = c.m_id;
- m_age = c.m_age;
- #ifdef TEST_WITH_SHALLOW_COPY
- //here is shallow copy
- m_name = c.m_name;
- #else
- //here is deep copy
- if(NULL != c.m_name)
- {
- m_name = new char[strlen(c.m_name)+ 1];
- strcpy(m_name, c.m_name);
- }
- #endif
- }
- //类型转换构造函数,根据一个指定的类型的对象创建一个本类的对象
- //例如:下面将根据一个double类型的对象创建了一个Complex对象
- student::student(int r)
- {
- cout << "call type cast constructor function ~" << endl;
- m_id = r;
- m_age = 20;
- m_name = new char[20];
- strcpy(m_name, "default_name");
- }
- //等号运算符重载
- //注意,这个类似复制构造函数,将=右边的本类对象的值复制给等号左边的对象,它不属于构造函数,等号左右两边的对象必须已经被创建
- //若没有显示的写=运算符重载,则系统也会创建一个默认的=运算符重载,只做一些基本的拷贝工作
- student &operator=( const student &rhs )
- {
- cout << "= operator overloading ~" << endl;
- // 首先检测等号右边的是否就是左边的对象本,若是本对象本身,则直接返回
- if ( this == &rhs )
- {
- return *this;
- }
- //复制等号右边的成员到左边的对象中
- this->m_id = rhs.m_id;
- this->m_age = rhs.m_age;
- if(NULL != rhs.m_name)
- {
- this->m_name = new char[strlen(rhs.m_name)+ 1];
- strcpy(m_name, rhs.m_name);
- }
- // 把等号左边的对象再次传出
- // 目的是为了支持连等 eg:a=b=c 系统首先运行 b=c
- // 然后运行 a= ( b=c的返回值,这里应该是复制c值后的b对象)
- return *this;
- }
- void dump_student_detail_info(void);
- };
- void student::dump_student_detail_info(void)
- {
- cout << "id : " << this->m_id<< endl;
- cout << "age : " << this->m_age << endl;
- cout << "name : " << this->m_name<< endl;
- }
- student test1(const student& c)
- {
- return c;
- }
- student test2(const student c)
- {
- return c;
- }
- student test3()
- {
- static student c(3, 22, "james");
- return c;
- }
- student& test4()
- {
- static student c(4, 20, "foolbar");
- return c;
- }
- void main()
- {
- cout << "first test ============= " << endl << endl;
- student c1, c2; //call no-param constructor
- student c3(1, 25, "stu1"); //call common(param) constructor
- //student c3 = student(1.0,2.5); //another type of call common(param) constructor
- c1 = c3; //call = operator overloading as object c1 has been construct
- c2 = 5.2; //first call type cast constructor then call = operator overloading and last call destructor
- //tow type of calling copy constructor.
- student c5(c2);
- student c4 = c2; //differ from = operator overloading as object c4 has't been contruct.
- c2.dump_student_detail_info();
- cout << endl << "second test ============= " << endl << endl;
- student a, b;
- //tell me the times that constructor called by downside function, and what type of construtor
- test1(a); //call copy constructor once
- test2(a); //call copy constructor twice, need
- b = test3(); //call common construtor once and copy construtor once
- b = test4(); //call common construtor once
- test2(12); //call type cast construtor once and copy construtor once
- test1(12); //call type cast construtor once and copy construtor once
- test1(student(12)); //call type cast construtor once and copy construtor once
- b.dump_student_detail_info();
- }
- //该片段来自于http://www.codesnippet.cn/detail/0312201411173.html
来源: http://www.codesnippet.cn/detail/0312201411173.html