指针学习1--new &delete & destructor
1. 一般指针的new&delete
对于指针a,delete a之后,指针a 的地址仍然是原来的地址(并不是NULL),只不过所指向的对象被释放了,此时指针存放的值为随机的,由编译器确定。
1
int *a = new int(2);
2
cout << "after new :" << endl;
3
cout << "a = " << a << "," << "*a = " << *a << endl;
4
5
delete a;
6
cout << "after delete: " << endl;
7
if(a == NULL)
8
{
9
cout << "a is null after delete" << endl;
10
}
11
else
12
{
13
cout << " a is not null after delete" << endl;
14
}
15
cout << "a = " << a << "," << "*a = " << *a << endl;
int *a = new int(2);2
cout << "after new :" << endl;3
cout << "a = " << a << "," << "*a = " << *a << endl;4

5
delete a;6
cout << "after delete: " << endl;7
if(a == NULL)8
{9
cout << "a is null after delete" << endl;10
}11
else12
{13
cout << " a is not null after delete" << endl;14
}15
cout << "a = " << a << "," << "*a = " << *a << endl;

//好的编程习惯
delete a;
a = NULL;
常规方法创建的对象,当实际对象(而不是对象的引用)超出作用域时,才会运行析构函数
动态方法创建的对象,当删除指向动态分配对象的指针时,才会运行析构函数。
2. 一般类对象的声明与初始化:不用new也可以定义类对象(区别于java),默认调析构函数
注:为方便简单演示,并没有遵循三法则
1
class Stu
2
{
3
public:
4
Stu(int m):var(m)
5
{
6
var = m;
7
cout << "constructor called." << endl;
8
}
9
~Stu() { cout << var << " destructor called." << endl;}
10
private:
11
int var;
12
};
13
14
void func()
15
{
16
//调用构造函数
17
Stu a(20);
18
//调用默认复制构造函数
19
Stu b(a);
20
//等价于Stu temp(b); Stu b(temp);
21
//所以有调用构造函数的过程
22
Stu c = 30;
23
//都是用隐式方式定义的,所以不用delete,自动调用析构函数
24 | //注意析构的顺序
25
}
26
27
int main()
28
{
29
func();
30
return 0;
31
}
class Stu2
{3
public:4
Stu(int m):var(m)5
{ 6
var = m;7
cout << "constructor called." << endl;8
} 9
~Stu() { cout << var << " destructor called." << endl;} 10
private:11
int var; 12
};13

14
void func()15
{16
//调用构造函数17
Stu a(20);18
//调用默认复制构造函数19
Stu b(a); 20
//等价于Stu temp(b); Stu b(temp); 21
//所以有调用构造函数的过程22
Stu c = 30; 23
//都是用隐式方式定义的,所以不用delete,自动调用析构函数24 | //注意析构的顺序
25
}26

27
int main()28
{29
func(); 30
return 0;31
}

这里有一个需要注意的地方,就是如果func里面的代码是直接写在main里的则不一定调用析构函数 ,由编译器决定。
上面是dev c++, 下面是vc6.0的运行结果
1
2
int main()
3
{
4
5
//{
6
//调用构造函数
7
Stu a(20);
8
//调用默认复制构造函数
9
Stu b(a);
10
//等价于Stu temp(b); Stu b(temp);
11
//所以有调用构造函数的过程
12
Stu c = 30;
13
//new出来的指针对象,必须显示delete,如a,b
14
//类对象结束局部范围后会自动调用析构函数 如c
15
//}
16
return 0;
17
}

2
int main()3
{4

5
//{6
//调用构造函数7
Stu a(20);8
//调用默认复制构造函数9
Stu b(a); 10
//等价于Stu temp(b); Stu b(temp); 11
//所以有调用构造函数的过程12
Stu c = 30; 13
//new出来的指针对象,必须显示delete,如a,b14
//类对象结束局部范围后会自动调用析构函数 如c 15
//}16
return 0;17
}

3. 用new定义类对象,必须delete
动态分配的对象只有在指向该对象的指针被删除时才撤销。如果没有删除指向动态对象的指针,则不会运行该对象的析构函数,对象则一直存在,从而导致内存泄露。
常规方法创建的对象,当实际对象(而不是对象的引用)超出作用域时,才会运行析构函数
动态方法创建的对象,当删除指向动态分配对象的指针时,才会运行析构函数。
1
class Stu
2
{
3
public:
4
Stu(int m):var(m)
5
{
6
var = m;
7
cout << "constructor called." << endl;
8
}
9
~Stu() { cout << var << " destructor called." << endl;}
10
private:
11
int var;
12
};
13
14
void func()
15
{
16
//调用构造函数
17
Stu* a = new Stu(20);
18
//调用默认复制构造函数
19
Stu* b= new Stu(*a);
20
//等价于Stu temp = new Stu(30); Stu b(temp);
21
//所以有调用构造函数的过程
22
Stu c = 30;
23
//new出来的指针对象,必须显示delete,如a,b
24
//类对象结束局部范围后会自动调用析构函数 如c
25
delete a;
26
delete b;
27
}
28
29
int main()
30
{
31
func();
32
return 0;
33
}
class Stu2
{3
public:4
Stu(int m):var(m)5
{ 6
var = m;7
cout << "constructor called." << endl;8
} 9
~Stu() { cout << var << " destructor called." << endl;} 10
private:11
int var; 12
};13

14
void func()15
{16
//调用构造函数17
Stu* a = new Stu(20);18
//调用默认复制构造函数19
Stu* b= new Stu(*a); 20
//等价于Stu temp = new Stu(30); Stu b(temp); 21
//所以有调用构造函数的过程22
Stu c = 30; 23
//new出来的指针对象,必须显示delete,如a,b24
//类对象结束局部范围后会自动调用析构函数 如c25
delete a;26
delete b;27
}28

29
int main()30
{31
func(); 32
return 0;33
}
转载请注明出处: http://www.cnblogs.com/liyuxia713/


浙公网安备 33010602011771号