程序如下:
1 class A
2 {
3 public:
4 A();
5 ~A();
6 int get()const{return *i;}
7 void set(int x){*i=x;}
8 private:
9 int *i;
10 };
11 int main()
12 {
13 A *p=new A;
14 cout<<p->get()<<endl;
15 p->set(0);
16 cout<<p->get()<<endl;
17 delete p;
18 return 0;
19 }
20 A::A()
21 {
22 cout<<"构造函数执行中...\n";
23 i=new int(999);
24 }
25 A::~A()
26 {
27 cout<<"析构函数执行中...\n";
28 delete i;
29 }
求教:第13行A *p=new A;调用对象的时候不是顺序调用A()函数和~A()函数吗?
还是说调用的顺序为:
第一步:
A();
int get()const{return *i;}
void set(int x){*i=x;}
cout<<p->get()<<endl;
p->set(0);
cout<<p->get()<<endl;
delete p;
return 0;
第二步:
~A();
int get()const{return *i;}
void set(int x){*i=x;}
cout<<p->get()<<endl;
p->set(0);
cout<<p->get()<<endl;
delete p;
return 0;
到底是怎么个执行步骤呢?求高手解答..
1 class A
2 {
3 public:
4 A();
5 ~A();
6 int get()const{return *i;}
7 void set(int x){*i=x;}
8 private:
9 int *i;
10 };
11 int main()
12 {
13 A *p=new A;
14 cout<<p->get()<<endl;
15 p->set(0);
16 cout<<p->get()<<endl;
17 delete p;
18 return 0;
19 }
20 A::A()
21 {
22 cout<<"构造函数执行中...\n";
23 i=new int(999);
24 }
25 A::~A()
26 {
27 cout<<"析构函数执行中...\n";
28 delete i;
29 }
求教:第13行A *p=new A;调用对象的时候不是顺序调用A()函数和~A()函数吗?
还是说调用的顺序为:
第一步:
A();
int get()const{return *i;}
void set(int x){*i=x;}
cout<<p->get()<<endl;
p->set(0);
cout<<p->get()<<endl;
delete p;
return 0;
第二步:
~A();
int get()const{return *i;}
void set(int x){*i=x;}
cout<<p->get()<<endl;
p->set(0);
cout<<p->get()<<endl;
delete p;
return 0;
到底是怎么个执行步骤呢?求高手解答..