标签:ios let names delete 自己的 失败 this 面向对象 面向
在我们进行面向对象程序设计的时候,我们肯定要设计自己的类,这样一来,我们就需要设计自己需要的构造函数和析构函数,那么我们可以通过指针直接调用构造函数和析构函数吗?
进行验证:
#include <iostream>
using namespace std;
//程序说明直接调用构造函数会出现错误,直接调用析构函数是成功的。
class A{
public:
int id;
A(int i):id(i){cout<<"ctor.this = "<<this<<" id = "<<id<<endl;
}
~A(){cout<<cout<<"dtor.this = "<<this;
}
};
int main3(){
A* pa = new A(1);
cout<<pa->id<<endl;
cout<<"直接调用构造函数失败:"<<endl;
//pa->A(1);
cout<<"直接调用析构函数成功:"<<endl;
//delete pa;
pa->~A() ;
return 0;
}
经过上述代码,我们发现,在类外直接调用构造函数是不行的,但是可以直接调用析构函数。
标签:ios let names delete 自己的 失败 this 面向对象 面向
原文地址:https://www.cnblogs.com/yjds/p/8948336.html