/* 时间:2014年11月4日20:40:45 内容:构造函数。 */ #include <iostream> using namespace std; class Test { public: int i; public: //成员函数的编写就是封装; //构造函数用来替换C语言中的大括号 读本类的数据进行初始化。 Test() { //memset(this,0,sizeof(A)); cout<<"构造函数的调用"<<endl; } Test(int i) { cout<<"单参数构造函数的调用"<<endl; this->i = i; } }; int main() { Test a; Test b(32);// 对于单参数函数 括号可以等价于 = ( A b = 32); Test f = 32; Test* c = new Test(32); cout<<c->i<<endl; Test* d = new Test; cout<<d->i<<endl; Test* e = &a; //不会调用构造函数,e只是一个简单的指针赋值的过程 int i = 32; int j(32); //在C++编译器中 认为普通变量的初始化也调用构造函数。 int *p = new int(32); //方括号代表申请元素的个数,小括号代表单参数的构造函数的调用 return 0; }