红莲教主 发表于 2016-5-12 17:18:42

指针函数

#include <iostream>
#include <string>

using std::cout ;
using std::endl ;
using std::string ;

int* newInt(int value);
double* newDouble(double value);

int main()
{
    int* pInt = newInt(500);

    double* pDouble = newDouble(33.33);

    cout<<"*pInt = "<<*pInt <<endl; //正确,堆内存只有使用delete删除

    cout<<"pDouble = "<<*pDouble<<endl; //错误,局部变量已经失效

    delete pInt ;
    pInt = NULL ;

    return 0;
}


int* newInt(int value)
{
    int* pInt = new int ;
    *pInt = value ;
    return pInt ;
}

double* newDouble(double value)
{
    double num = value;

    return &num ;
}
页: [1]
查看完整版本: 指针函数