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