对象指针的用法

// 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

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值