#include<iostream>#include<string>usingnamespace std;classTest{public:Test(){}virtual~Test(){}};template<typename T >charIsPtr(T* v){return'0';}intIsPtr(...){return0;}#define ISPTR(p) ((sizeof(IsPtr(p))) == sizeof(char))intmain(int argc,char*argv[]){int i =0;int* p =&i;
cout <<"p is a pointer: "<<ISPTR(p)<< endl;
cout <<"i is Not a pointer: "<<ISPTR(i)<< endl;
Test t;
Test* pt =&t;
cout <<"pt is a pointer: "<<ISPTR(pt)<< endl;
cout <<"t is Not a pointer: "<<ISPTR(t)<< endl;return0;}
构造函数抛出异常
构造函数抛出异常
构造过程立即停止
对象无法生成
析构函数不会被调用
对象锁占用的空间立即收回
工程建议
不要在构造函数中抛出异常
当构造函数那会可能产生异常时,使用二阶构造模式
#include<iostream>#include<string>usingnamespace std;classTest{public:Test(){
cout <<"Test()"<< endl;throw0;}virtual~Test(){
cout <<"virtual ~Test()"<< endl;}};intmain(int argc,char*argv[]){
Test* p =reinterpret_cast<Test*>(1);try{
p =newTest();}catch(...){
cout <<"Expection..."<< endl;}
cout <<"p = "<< p << endl;return0;}