@lishuhuakai
2015-05-21T16:55:36.000000Z
字数 3923
阅读 2014
c++
/*
1 C++中提供了初始化列表对成员变量进行初始化
2 使用初始化列表出现原因:
1.必须这样做:
如果我们有一个类成员,它本身是一个类或者是一个结构,而且这个成员它只有一个带参数的构造函数,
而没有默认构造函数,这时要对这个类成员进行初始化,就必须调用这个类成员的带参数的构造函数,
如果没有初始化列表,那么他将无法完成第一步,就会报错。
2、类成员中若有const修饰,必须在对象初始化的时候,给const int m 赋值
当类成员中含有一个const对象时,或者是一个引用时,他们也必须要通过成员初始化列表进行初始化,
因为这两种对象要在声明后马上初始化,而在构造函数中,做的是对他们的赋值,这样是不被允许的。
*/
//总结 构造和析构的调用顺序
#include "iostream"
using namespace std;
class ABC
{
public:
ABC(int a, int b, int c)
{
this->a = a;
this->b = b;
this->c = c;
printf("==========addr:%x===============\n", this);
printf("a:%d,b:%d,c:%d \n", a, b, c);
printf("ABC construct ..\n");
printf("================================\n");
}
~ABC()
{
printf("==========addr:%x===============\n", this);
printf("a:%d,b:%d,c:%d \n", a, b, c);
printf("~ABC() ..\n");
printf("================================\n");
}
protected:
private:
int a;
int b;
int c;
};
class MyD
{
public:
MyD() :abc1(1, 2, 3), abc2(4, 5, 6), m(100)
{
printf("==========addr:%x===============\n", this);
cout << "MyD()" << endl;
printf("================================\n");
}
~MyD()
{
printf("==========addr:%x===============\n", this);
cout << "~MyD()" << endl;
printf("================================\n");
}
protected:
private:
ABC abc1; //c++编译器不知道如何构造abc1
ABC abc2;
const int m;
};
int run()
{
MyD myD;
return 0;
}
int main()
{
run();
system("pause");
return 0;
}
/**
==========addr:3ffd60=============
a:1,b:2,c:3
ABC construct ..
================================
==========addr:3ffd6c=============
a:4,b:5,c:6
ABC construct ..
================================
==========addr:3ffd60=============
MyD()
================================
==========addr:3ffd60=============
~MyD()
================================
==========addr:3ffd6c=============
a:4,b:5,c:6
~ABC() ..
================================
==========addr:3ffd60=============
a:1,b:2,c:3
~ABC() ..
================================
*/
其实很好理解,调用子函数时会做压栈处理,子函数里面的变量也按照栈的思想来理解即可,先入栈的先调用构造函数,又因为栈的先进后出的特性,因此,先进栈的后析构。
可以好好分析一下下面的代码和结果:
//对象做函数参数
//1 研究拷贝构造
//2 研究构造函数,析构函数的调用顺序
//总结 构造和析构的调用顺序
#include <iostream>
using namespace std;
class ABCD
{
public:
ABCD(int a, int b, int c)
{
this->a = a;
this->b = b;
this->c = c;
printf("==========addr:%x==========\n", this);
printf("ABCD()构造函数, a:%d,b:%d,c:%d \n", this->a, this->b, this->c);
}
~ABCD()
{
printf("==========addr:%x==========\n", this);
printf("~ABCD()析构函数,a:%d,b:%d,c:%d \n", this->a, this->b, this->c);
}
int getA()
{
return this->a;
}
protected:
private:
int a;
int b;
int c;
};
class MyE
{
public:
MyE() :abcd1(1, 2, 3), abcd2(4, 5, 6), m(100)
{
printf("==========addr:%x==========\n", this);
cout << "MyE无参构造函数" << endl;
}
~MyE()
{
printf("==========addr:%x==========\n", this);
cout << "MyE析构函数" << endl;
}
MyE(const MyE & obj) :abcd1(7, 8, 9), abcd2(10, 11, 12), m(100)
{
printf("==========addr:%x==========\n", this);
printf("MyD拷贝构造\n");
}
public:
ABCD abcd1;
ABCD abcd2;
const int m;
};
int doThing(MyE mye1)
{
//printf("doThing函数");
cout << endl;
return 0;
}
int run2()
{
MyE myE;
doThing(myE);
return 0;
}
int run3()
{
printf("run3 start..\n");
//ABCD abcd = ABCD(100, 200, 300);
//若直接调用构造函数哪
//想调用构造函数对abc对象进行再复制,可以吗?
//在构造函数里面调用另外一个构造函数,会有什么结果?
ABCD(400, 500, 600); //临时对象的生命周期
printf("run3 end\n");
return 0;
}
int main()
{
run2();
//run3();
system("pause");
return 0;
}
/**
ABCD()构造函数, a:1,b:2,c:3
==========addr:2cf8c4==========
ABCD()构造函数, a:4,b:5,c:6
==========addr:2cf8b8==========
MyE无参构造函数
==========addr:2cf7ac==========
ABCD()构造函数, a:7,b:8,c:9
==========addr:2cf7b8==========
ABCD()构造函数, a:10,b:11,c:12
==========addr:2cf7ac==========
MyD拷贝构造
==========addr:2cf7ac==========
MyE析构函数
==========addr:2cf7b8==========
~ABCD()析构函数,a:10,b:11,c:12
==========addr:2cf7ac==========
~ABCD()析构函数,a:7,b:8,c:9
==========addr:2cf8b8==========
MyE析构函数
==========addr:2cf8c4==========
~ABCD()析构函数,a:4,b:5,c:6
==========addr:2cf8b8==========
~ABCD()析构函数,a:1,b:2,c:3
*/
也可以顺带分析一下下面的代码:
#include "iostream"
using namespace std;
class MyTest
{
public:
MyTest(int a, int b, int c)
{
this->a = a;
this->b = b;
this->c = c;
}
MyTest(int a, int b)
{
this->a = a;
this->b = b;
MyTest(a, b, 100);
}
~MyTest()
{
printf("MyTest~:%d, %d, %d\n", a, b, c);
}
protected:
private:
int a;
int b;
int c;
public:
int getC() const { return c; }
void setC(int val) { c = val; }
};
int main()
{
MyTest t1(1, 2);
printf("c:%d", t1.getC()); //请问c的值是?
system("pause");
return 0;
}
/**
MyTest~:1, 2, 100
c:-858993460
*/