
函数调用链
1 |
#include<iostream> |
Output:
1 |
x = 10 y = 20 |
对于一个类X,this指针是X const
如果一个成员函数X被声明为const,this指针为const X const
1 |
#include<iostream> |
delete最好不要用在this指针上面,如果使用,需要考虑下面的情况
-
delete操作只对使用了new的对象有效
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24class A
{
public:
void fun()
{
delete this;
}
};
int main()
{
/* Following is Valid */
A *ptr = new A;
ptr->fun();
ptr = NULL // make ptr NULL to make sure that things are not accessed using ptr.
/* And following is Invalid: Undefined Behavior */
A a;
a.fun();
getchar();
return 0;
} -
delete之后,所有的成员都不可以访问
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include<iostream>
using namespace std;
class A
{
int x;
public:
A() { x = 0;}
void fun() {
delete this;
/* Invalid: Undefined Behavior */
cout<<x;
}
};




近期评论