构造函数中指针参数的初始化
2012-03-24 15:51
155 查看
最近在写一个小程序时,主要是文件信息的读取。
在程序中定义了类Tperson
class Tperson
{
public:
Tperson(string newName=NULL ,int newAge=0);
virtual~Tperson();
private:
string name;
int age;
public:
void SetName(string newName);
voidSetAge(int newAge);
stringGetName();
intGetAge();
};
person::Tperson(string newName,int newAge)
{
name=newName;
age=newAge;
}
Tperson::~Tperson()
{
}
int Tperson::GetAge()
{
returnage;
}
string Tperson::GetName()
{
returnname;
}
void Tperson::SetAge(int newAge)
{
age=newAge;
}
void Tperson::SetName(string newName)
{
name=newName;
}
然后定义两个类数组
Tpersonap1[3]={Tperson("wang",26),Tperson("zhao",29),Tperson("huang",21)};
Tperson ap2[3];
主程序如下
int main(int argc,char*argv[])
{
inti;
ofstreamofile;
ofile.open("data.text",ios::out);
if(!ofile)
{
cout<<"can'topen output file"<<endl;
return-1;
}
for(i=0;i<3;i++)
{
ofile.write((char*)&ap1[i],sizeof(ap1[i]));
}
ofile.close();
ifstreamifile;
ifile.open("data.text",ios::in|ios::binary);
if(!ifile)
{
cout<<"can'topen input file"<<endl;
return-1;
}
for(i=0;i<3;i++)
{
ifile.read((char*)&ap2[i],sizeof(ap2[i]));
}
ifile.close();
for(i=0;i<3;i++)
{
cout<<"nameof person "<<i+1<<" is"<<ap2[i].GetName()<<endl;
cout<<"ageof person "<<i+1<<" is"<<ap2[i].GetAge()<<endl;
}
return0;
}
编译完全通过,可是一运行就出错,内存不能读。纠结好了好久,这可是按书上面的例子编写的。
最后实在不行了,一行一行代码的查找,发现出错在Tperson ap2[3]上。因为这个类是没有初始化的,只能调用默认构造函数,可是默认构造函数中的string newName=NULL,这样表面上看是给了默认值,实际上该参数仍然是不定的。
将stringnewName=NULL改为stringnewName=””。
重新编译,运行OK.。
C++中的指针真难伺候啊
在程序中定义了类Tperson
class Tperson
{
public:
Tperson(string newName=NULL ,int newAge=0);
virtual~Tperson();
private:
string name;
int age;
public:
void SetName(string newName);
voidSetAge(int newAge);
stringGetName();
intGetAge();
};
person::Tperson(string newName,int newAge)
{
name=newName;
age=newAge;
}
Tperson::~Tperson()
{
}
int Tperson::GetAge()
{
returnage;
}
string Tperson::GetName()
{
returnname;
}
void Tperson::SetAge(int newAge)
{
age=newAge;
}
void Tperson::SetName(string newName)
{
name=newName;
}
然后定义两个类数组
Tpersonap1[3]={Tperson("wang",26),Tperson("zhao",29),Tperson("huang",21)};
Tperson ap2[3];
主程序如下
int main(int argc,char*argv[])
{
inti;
ofstreamofile;
ofile.open("data.text",ios::out);
if(!ofile)
{
cout<<"can'topen output file"<<endl;
return-1;
}
for(i=0;i<3;i++)
{
ofile.write((char*)&ap1[i],sizeof(ap1[i]));
}
ofile.close();
ifstreamifile;
ifile.open("data.text",ios::in|ios::binary);
if(!ifile)
{
cout<<"can'topen input file"<<endl;
return-1;
}
for(i=0;i<3;i++)
{
ifile.read((char*)&ap2[i],sizeof(ap2[i]));
}
ifile.close();
for(i=0;i<3;i++)
{
cout<<"nameof person "<<i+1<<" is"<<ap2[i].GetName()<<endl;
cout<<"ageof person "<<i+1<<" is"<<ap2[i].GetAge()<<endl;
}
return0;
}
编译完全通过,可是一运行就出错,内存不能读。纠结好了好久,这可是按书上面的例子编写的。
最后实在不行了,一行一行代码的查找,发现出错在Tperson ap2[3]上。因为这个类是没有初始化的,只能调用默认构造函数,可是默认构造函数中的string newName=NULL,这样表面上看是给了默认值,实际上该参数仍然是不定的。
将stringnewName=NULL改为stringnewName=””。
重新编译,运行OK.。
C++中的指针真难伺候啊
相关文章推荐
- 二级指针作为函数参数传递初始化的一点总结
- C++ 基类构造函数带参数的继承方式及派生类的初始化
- Qt ------ 初始化构造函数参数,parent
- 第五周 项目1 (2) 在构造函数中使用参数初始化表对数据初始化
- C++ 几种对象数组初始化(带参数的构造函数)
- 定义构造函数时必须使用引用参数或指针参数
- 第五周项目1.4在构造函数中使用参数初始化表对数据成员初始化
- 第4周项目1-三角形类的构造函数(4)-使用参数初始化表
- 第四周 项目1三角形类的构造函数(3)---参数初始化表
- 初始化与清理(构造函数初始化,可变参数列表,enum)
- 第五周项目一构造函数————4)参数初始化表
- C++第5周任务1-5在构造函数中使用参数初始化表对数据成员初始化。
- C++构造函数的自动调用(调用一个父类的构造函数,有显性调用最好,否则就默认调用无参数的构造函数)——哲学思想:不调用怎么初始化父类的成员数据和VMT?
- 第四周项目1————(3)在构造函数中使用参数对数据成员初始化
- [C++基础]在子类中向父类的构造函数传递参数的小例子,包括类中常量的初始化
- 三角形类的构造函数-使用参数初始化表对数据成员初始化
- 第四周项目1:三角形类的构造函数:参数初始化表
- c++笔记03---构造函数,初始化表,文件分类,钟表练习,析构函数,this 指针
- [C++基础]在子类中向父类的构造函数传递参数的小例子,包括类中常量的初始化
- 构造函数 参数初始化列表