#include
#include
using namespace std;
class String{
private:
char*m_data;
public:
String(const char * str=NULL);
String(const String & other);
~String();
};
String::String(const char * str){
if(str!=NULL){
m_data=new char[strlen(str)+1];
strcpy(m_data,str);
}
else{
m_data=new char[8];
strcpy(m_data,"no char");
}
cout<<*m_data<<" constructing!"<<endl;
}
String::String(const String&other){
if(other.m_data!=NULL)
{
m_data=new char[strlen(other.m_data)+1];
strcpy(m_data,other.m_data);
}
else{
m_data=new char [8];
strcpy(m_data,"no char");
}
cout<<*m_data<<" copy constructing!"<<endl;
}
String::~String(){
cout<<*m_data<<" destructing!"<<endl;
delete[]m_data;
}
int main(){
String s1("family");
String s2=s1;
return 0;
}
这个程序的结果为什么只能输出字符串的第一个字符?
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
3条回答 默认 最新
- linux_zhu 2015-03-22 10:52关注
你使用的是*m_data 而不是m_data
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 Visual Studio问题
- ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
- ¥20 求一个html代码,有偿
- ¥100 关于使用MATLAB中copularnd函数的问题
- ¥20 在虚拟机的pycharm上
- ¥15 jupyterthemes 设置完毕后没有效果
- ¥15 matlab图像高斯低通滤波
- ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
- ¥15 钢筋实图交点识别,机器视觉代码
- ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)