构造和析构函数没输出
#include<iostream>#include<string.h>
using namespace std;
const int bufsize=64;
class String{
char *buf;
int size;
public:
bool copy(char *str);
char *get();
String();
~String();
};
String::String(){
size=bufsize;
buf=new char;
cout<<"aaa"<<endl;
}
bool String::copy(char *str){
if(str=NULL) return 0;
if((strlen(str)+1)>size) return 0;
strcpy(buf,str);
return true;
}
char *String::get(){
return buf;
}
String::~String(){
if(buf!=NULL)
delete []buf;
cout<<"aaa"<<endl;
}
int main(){
String str;
str.copy("hello");
char *p=str.get();
if(p=NULL) cout<<"str is NULL"<<endl;
else cout<<"str is"<<p<<endl;
return 0;
}
输出结果 aaa 然后结束,求大佬看看哪里错了 两个地方修改
bool String::copy(char *str)
{
//str = NULL 是赋值,改成 str == NULL
if(str==NULL) return 0;
if((strlen(str)+1)>size) return 0;
strcpy(buf,str);
return true;
}
int main(){
String str;
str.copy("hello");
char *p=str.get();
//p = NULL是赋值,改成 p==NULL
if(p==NULL) cout<<"str is NULL"<<endl;
else cout<<"str is "<<p<<endl;
return 0;
}
页:
[1]