1.构造函数、析构函数与拷贝构造函数介绍
构造函数
构造函数不能有返回值
缺省构造函数时, 系统将自动调用该缺省构造函数初始化对象,缺省构造函数会将所有数据成员都初始化为零或空
创建一个对象时,系统自动调用构造函数
析构函数
析构函数没有参数,也没有返回值。不能重载,也就是说,一个类中只可能定义一个析构函数
如果一个类中没有定义析构函数,系统也会自动生成一个默认的析构函数,为空函数,什么都不做
调用条件:1.在函数体内定义的对象,当函数执行结束时,该对象所在类的析构函数会被自动调用;2.用new运算符动态构建的对象,在使用delete运算符释放它时。
拷贝构造函数
拷贝构造函数实际上也是构造函数,具有一般构造函数的所有特性,其名字也与所属类名相同。拷贝构造函数中只有一个参数,这个参数是对某个同类对象的引用。它在三种情况下被调用:
用类的一个已知的对象去初始化该类的另一个对象时;
函数的形参是类的对象,调用函数进行形参和实参的结合时;
函数的返回值是类的对象,函数执行完返回调用者。
【代码】
/*
version: 1.0
author: hellogiser
blog: http://www.cnblogs.com/hellogiser
date: 2014/9/25
*/
#include "stdafx.h"
#include <iostream>
using namespace std;
class point
{
private:
int x, y;
public:
point(int xx = 0, int yy = 0)
{
x = xx;
y = yy;
cout << "Constructor" << endl;
}
point(const point &p)
{
x = p.x;
y = p.y;
cout << "Copy Constructor" << endl;
}
~point()
{
cout << "Destructor" << endl;
}
int get_x()
{
return x;
}
int get_y()
{
return y;
}
};
void f(point p)
{
// copy constructor
cout << p.get_x() << " " << p.get_y() << endl;
// destructor
}
point g()
{
point a(7, 33); //constructor
return a; // copy constructor temp object
}
void test()
{
point a(15, 22); // constructor
point b(a); //(1) copy constructor
cout << b.get_x() << " " << b.get_y() << endl; // 15 22
f(b);// (2) copy constructor
b = g(); // (3) copy constructor
cout << b.get_x() << " " << b.get_y() << endl; // 7 33
}
int main()
{
test();
return 0;
}
/*
Constructor
Copy Constructor
15 22
Copy Constructor
15 22
Destructor
Constructor
Copy Constructor
Destructor
Destructor
7 33
Destructor
Destructor
*/
2. 继承关系中构造函数执行顺序
(1)任何虚拟基类(virtual)的构造函数按照它们被继承的顺序构造;
(2)任何非虚拟基类(non-virtual)的构造函数按照它们被继承的顺序构造;
(3)任何成员对象(data member)的构造函数按照它们声明的顺序调用;
(4)类自己的构造函数(self)。
【代码】
/*
version: 1.0
author: hellogiser
blog: http://www.cnblogs.com/hellogiser
date: 2014/9/27
*/
#include "stdafx.h"
#include <iostream>
using namespace std;
class OBJ1
{
public:
OBJ1()
{
cout << "OBJ1\n";
}
};
class OBJ2
{
public:
OBJ2()
{
cout << "OBJ2\n";
}
};
class Base1
{
public:
Base1()
{
cout << "Base1\n";
}
};
class Base2
{
public:
Base2()
{
cout << "Base2\n";
}
};
class Base3
{
public:
Base3()
{
cout << "Base3\n";
}
};
class Base4
{
public:
Base4()
{
cout << "Base4\n";
}
};
class Derived : public Base1, virtual public Base2,
public Base3, virtual public Base4
{
public:
Derived() : Base4(), Base3(), Base2(),
Base1(), obj2(), obj1()
{
cout << "Derived.\n";
}
protected:
OBJ1 obj1;
OBJ2 obj2;
};
void test()
{
Derived aa;
cout << "This is ok.\n";
}
int main()
{
test();
return 0;
}
/*
Base2
Base4
Base1
Base3
OBJ1
OBJ2
Derived.
This is ok.
*/
【代码2】
/*
version: 1.0
author: hellogiser
blog: http://www.cnblogs.com/hellogiser
date: 2014/9/27
*/
#include "stdafx.h"
#include <iostream>
using namespace std;
class Base1
{
public:
Base1(int i)
{
cout << "Base1 " << i << endl;
}
};
class Base2
{
public:
Base2(int i)
{
cout << "Base2 " << i << endl;
}
};
class Base3
{
public:
Base3()
{
cout << "Base3 *" << endl;
}
};
class Derived : public Base2, public Base1, virtual public Base3
{
public:
Derived(int a, int b, int c, int d, int e)
: Base1(a), b2(d), b1(c), Base2(b)
{
m = e;
cout << "Derived.\n";
}
protected:
Base1 b1;
Base2 b2;
Base3 b3;
int m;
};
void test()
{
Derived aa(1, 2, 3, 4, 5);
cout << "This is ok.\n";
}
int main()
{
test();
return 0;
}
/*
Base3 *
Base2 2
Base1 1
Base1 3
Base2 4
Base3 *
Derived.
This is ok.
*/
分析:
(1) virtual
按照继承顺序:Base3
第一步:先继承Base3,在初始化列表里找不到Base3(), 则调用Base3里的默认构造函数Base3(),打印"Base3 *"
(2)non-virtual
按照继承顺序:Base2,Base1
第二步:继承Base2,在初始化列表中找Base2(b),调用Base2的构造函数Base2(2),打印"Base2 2"
第三步:继承Base1,在初始化列表中找Base1(a),调用Base1的构造函数Base1(1),打印"Base1 1"
(3)data member
按照申明顺序:b1,b2,b3
第四步:构造b1,在初始化列表中找b1(c),调用Base1的构造函数Base1(3),打印"Base1 3"
第五步:构造b2,在初始化列表中找b2(d),调用Base2的构造函数Base1(4),打印"Base2 4"
第六步:构造b3,在初始化列表中找不到b3(),调用Base3的构造函数Base3(),打印"Base3 *"
(4)self
第7步:执行自己的构造函数体,输出"Derived."
|