int i(222); // definition, initialized int *pi = new int(222) // dynamically, initialized
string *ps = new string; // initialized to empty string int *pi = new int; // pi points to an uninitialized int
string *ps2 = new string(); // initialized to empty string int *pi = new int(); // pi points to an int value-initialized to 0
int myValue(); // not value-initialized int variable, but a function named myValue int *myPtr = new int(); // correct! a value-initialized int object
// allocate and initialize a const object const int *pci = new const int(222); // initialize to 222 const int *pci2 = new const int(); // initialize to 0
#include <iostream>
#include <cstring>
using namespace std;
int main(void)
{
string str = "hello str";
// strNew points to dynamically allocated,
// initialized to empty string
string *strNew = new string;
cout<<"str object address: "<<&str<<endl;
cout<<"strNew pointer itself address: "<<&strNew<<endl;
cout<<"strNew pointer to address: "<<strNew<<endl;
// assignment
*strNew = "hello strNew";
cout<<"strNew pointer to address: "<<strNew<<endl;
// free memory
delete strNew;
cout<<"strNew pointer to address: "<<strNew<<endl;
strNew = NULL;
// point to other object
strNew = &str;
cout<<"strNew pointer to address: "<<strNew<<endl;
const int cvalue(10);
// iptr points to a const int object
const int *iptr = new const int(222);
cout<<"iptr value: "<<*iptr<<endl;
delete iptr;
iptr = NULL;
iptr = &cvalue;
cout<<"iptr value: "<<*iptr<<endl;
return 0;
}
str object address: 0x28ff24 strNew pointer itself address: 0x28ff20 strNew pointer to address: 0x602f70 strNew pointer to address: 0x602f70 strNew pointer to address: 0x602f70 strNew pointer to address: 0x28ff24 iptr value: 222 iptr value: 10
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有