|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #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 ;
- }
复制代码 |
|