// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
//下面先定义一个类
class haitao{
private :
int agee;
public :
int getAgee(){
return agee;
}
haitao(){ agee = 30; }
haitao(int aaa): agee(aaa){}
};
int _tmain(int argc, _TCHAR* argv[])
{
haitao * tp01;
tp01 = new haitao(35);
cout<<tp01->getAgee()<<endl;
haitao *tp02;
tp02 = new haitao;
//上面这一行等同于to02=new haitao();
cout << tp02->getAgee() << endl;
system("pause");
return 0;
}
总结:
当我们使用关键字new在堆上动态创建一个对象时,它实际上做了三件事:
1 获得一块内存空间
2 调用构造函数
3 返回正确的指针
当然,如果我们创建的是简单类型的变量,那么第二步会被省略
FR:海涛高软(hunk xu) QQ技术交流群:386476712