这个c++程序有一些问题,我是初学,不知问题在哪。求大神帮助!
class CStrtemp{
public:
CStrtemp(){};
CStrtemp(char *s);
CStrtemp(const CStrtemp &);
~CStrtemp();
CStrtemp Input(CStrtemp *temp);
void show();
void set(char *s);
private:
char *str;
};
CStrtemp::CStrtemp(char *s){
cout << "constructor." << endl;
str = new char[strlen(s) + 1];
str = "hello";
if(!str){
cerr << "Allocating Error." << endl;
exit(1);
}
strcpy(str, s);
}
CStrtemp::CStrtemp(const CStrtemp &temp){
cout << "copy constructor." << endl;
str = new char[strlen(temp.str) + 1];
if(!str){
cerr << "error in apply new space.r" << endl;
exit(1);
}
strcpy(str, temp.str);
}
CStrtemp::~CStrtemp(){
cout << "destructor." << endl;
if(str != NULL)
delete[]str;
}
void CStrtemp::show(){
cout << str << endl;
}
void CStrtemp::set(char *s){
delete[]s;
str = new char[strlen(s) + 1];
if(!str){
cerr << "Allocation Error." << endl;
exit(1);
}
strcpy(str, s);
}
CStrtemp CStrtemp::Input(CStrtemp *temp){ char *s;
s = new char[strlen(temp ->str) + 1];
cout << "Please input the string:" << endl;
cin >> s;
temp ->set(s);
return *temp;}
int main(){
CStrtemp C;
CStrtemp *A = &C;
A ->show();
CStrtemp B = A ->Input(A);
A ->show();
B.show();
return 0;} 展开
修改如下:
#include <iostream>
using namespace std;
class CStrtemp
{
public:
CStrtemp()
{};
CStrtemp(char *s);
腔友蚂CStrtemp(const CStrtemp &);
~CStrtemp();
CStrtemp Input(CStrtemp *temp);
void show();
void set(char *s);
private:
char *str;
};
CStrtemp::CStrtemp(char *s)
{
cout << "constructor." << endl;
str = new char[strlen(s) + 1];
str = "hello";
if(!str)
{
cerr << "Allocating Error." << endl;
exit(1);
}
strcpy(str, s);
}
CStrtemp::CStrtemp(const CStrtemp &temp)
{
cout << "copy constructor." << endl;
str = new char[strlen(temp.str) + 1];
if(!str)
{
cerr << "error in apply new space.r" << endl;
exit(1);
}
strcpy(str, temp.str);
}
CStrtemp::~CStrtemp()
{
cout << "destructor." << endl;
if(str != NULL)
delete[]str;
}
void CStrtemp::show()
{
cout << str << endl;
}
void CStrtemp::set(char *s)
{
/*你上来就把输入的数组删除了,那么该地址被释放,str就分配不告仔到空间了。因为你的str是根据s来生成的 删除这句 delete[]s; */
str = new char[strlen(s) + 1];
if(!str)
{
cerr <伍埋< "Allocation Error." << endl;
exit(1);
}
strcpy(str, s);
}
CStrtemp CStrtemp::Input(CStrtemp *temp)
{
char *s;
s = new char[strlen(temp ->str) + 1];
cout << "Please input the string:" << endl;
cin >> s;
temp ->set(s);
return *temp;
}
int main()
{
CStrtemp C;
CStrtemp *A = &C;
A ->set("class A set string");//加一句,因为你调用的是空构造函数,所以你show之前不设置的话,str指针是空的。这时你调用show会报错,
A ->show();
CStrtemp B = A ->Input(A);
A ->show();
B.show();
return 0;
}
结果如下图:
请问如果定义的时候用new s[...],和直接定义一个s[12]数组用数组名的效果是一样的吗,那用 *s呢是不是s此时就是没申请空间的野指针?十分感谢你
是一样的,只不过s[12]这后,s其实就是个常量了,
#include <iostream>
using namespace std;
class CStrtemp
{
public:
CStrtemp();
CStrtemp(char *s);
CStrtemp(const CStrtemp &);
~CStrtemp();
CStrtemp Input(CStrtemp *temp);
void show();
void set(char *s);
private:
char *str;
};
CStrtemp::CStrtemp()
{
cout << "constructor." << endl;
str = new char[strlen("帆裤hello") + 1];
str = "hello";
if(!str){
cerr << "Allocating Error." << endl;
exit(1);
}
}
CStrtemp::CStrtemp(char *s)
{
cout << "constructor." << endl;
str = new char[strlen(s) + 1];
//str = "hello";
if(!str){
cerr << "Allocating Error." <态备简< endl;
exit(1);
}
strcpy(str, s);
}
CStrtemp::CStrtemp(const CStrtemp &temp){
cout << "copy constructor." << endl;
str = new char[strlen(temp.str) + 1];
if(!str){
cerr << "error in apply new space.r"滚凯 << endl;
exit(1);
}
strcpy(str, temp.str);
}
CStrtemp::~CStrtemp(){
cout << "destructor." << endl;
if(str != NULL)
delete[]str;
}
void CStrtemp::show(){
cout << str << endl;
}
void CStrtemp::set(char *s){
// delete[]s;
str = new char[strlen(s) + 1];
if(!str){
cerr << "Allocation Error." << endl;
exit(1);
}
strcpy(str, s);
}
CStrtemp CStrtemp::Input(CStrtemp *temp){
char s[100];
// s = new char[strlen(temp ->str) + 1];
cout << "Please input the string:" << endl;
cin >> s;
temp->set(s);
return *temp;
}
int main(){
CStrtemp C;
CStrtemp *A = &C;
A->show();
CStrtemp B = A->Input(A);
A->show();
B.show();
return 0;
}
大神说的也是一种解决办法,也很感谢!