C++类相关要点

空间
| 1 2 3 4 5 6 7 8 9 10 11 12 13
 | class a{}; class b{}; class c:public a { 	virtual void fun()=0; }; class d:public b,public c{}; cout<<"sizeof(a)"<<sizeof(a)<<endl;  cout<<"sizeof(b)"<<sizeof(b)<<endl;  cout<<"sizeof(c)"<<sizeof(c)<<endl;  cout<<"sizeof(d)"<<sizeof(d)<<endl; 
 | 
使用32bit编译,结果如下:
| 1 2 3 4
 | sizeof(a)1 sizeof(b)1 sizeof(c)4 sizeof(d)4
 | 
使用64bit编译,结果如下:
| 1 2 3 4
 | sizeof(a)1 sizeof(b)1 sizeof(c)8 sizeof(d)8
 | 
注意:C++中指针本身的长度、long变量的长度是和内存地址位宽一致的
构造函数
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 | class Tran { public: 	string val; 	Tran(const string & str): val(str) 	{ 		cout<<"隐式转换函数调用"<<endl; 	}  	Tran(const Tran & tr): val(tr.val) 	{ 		cout<<"赋值构造函数调用"<<endl; 	}  	const Tran & operator= (const Tran & tr) 	{ 		cout<<"复制函数调用"<<endl; 		val = tr.val; 		return *this; 	} }; Tran tran2 = string("hello");  Tran tran3 = tran2;            tran3 = tran2;                 cout<<tran2.val<<endl;
 | 
输出如下:
| 1 2 3 4
 | 隐式转换函数调用 赋值构造函数调用 复制函数调用 hello
 |