c++面试题,请高人解答

liushu00 2005-07-13 04:07:39
六、编写类String的构造函数、析构函数和赋值函数(25分)
已知类String的原型为:
class String
{
public:
String(const char *str = NULL);// 普通构造函数
String(const String &other); // 拷贝构造函数
~ String(void); // 析构函数
String & operate =(const String &other);// 赋值函数
private:
char *m_data;// 用于保存字符串
};
请编写String的上述4个函数。
...全文
2843 36 打赏 收藏 转发到动态 举报
写回复
用AI写文章
36 条回复
切换为时间正序
请发表友善的回复…
发表回复
lnp 2006-06-28
  • 打赏
  • 举报
回复
m
sunlu_eric 2005-07-16
  • 打赏
  • 举报
回复
String(const String &other) // 拷贝构造函数
{
cout<<other.m_data; //这里要调用公共方法访问,不能直接访问。
int len = strlen(other.m_data);
m_data = new char[len+1];
strcpy(m_data,other.m_data); //这里要调用公共方法访问,不能直接访问。

}
cout<<other.m_data;//m_data是other对象的私有数据,怎么可以这样得到呢?请指教。
/* 类的成员函数当然可以访问类的私有数据 */
就是,因为他是成员函数呀,默认的this指针那!
xjp6688 2005-07-15
  • 打赏
  • 举报
回复
这种题挺基础
psc88 2005-07-15
  • 打赏
  • 举报
回复
高质量编程中全部都有的啊
zzm7000 2005-07-15
  • 打赏
  • 举报
回复
这几天面试题特别多哈
迈克暖风 2005-07-15
  • 打赏
  • 举报
回复
是不是又照搬林锐的《c++高质量编程》后的习题啊^_^
netfloator 2005-07-14
  • 打赏
  • 举报
回复
#include<iostream.h>
#include<string.h>
class String
{
public:
String(const char *str = NULL)// 普通构造函数
{
if(str==NULL)
{
m_data = new char[1]; // 若能加 NULL 判断则更好
*m_data = '\0';
}
else
{
int length = strlen(str);
m_data = new char[length+1]; // 若能加 NULL 判断则更好
strcpy(m_data, str);
}

}
String(const String &other) // 拷贝构造函数
{
int len = strlen(other.m_data);
m_data = new char[len+1];
strcpy(m_data,other.m_data);

}
virtual ~ String(void) // 析构函数
{
if(m_data)
delete []m_data;
}
String & operator =(const String &other)// 赋值函数
{

if(this == &other)
return *this;

// (2) 释放原有的内存资源 // 3分
delete [] m_data;

// (3)分配新的内存资源,并复制内容 // 3分
int length = strlen(other.m_data);
m_data = new char[length+1]; // 若能加 NULL 判断则更好
strcpy(m_data, other.m_data);

// (4)返回本对象的引用 // 3分
return *this;

}


void Display()
{
cout<<m_data<<endl;
}
private:
char *m_data;// 用于保存字符串
};
int main()
{
String a("abc"),b;
b = a;
b.Display();
return 0;
}
shuihan84 2005-07-14
  • 打赏
  • 举报
回复
又是林锐的题
chengjr 2005-07-14
  • 打赏
  • 举报
回复
恩 考这个题的就是垃圾
我也做过这个题,可惜当时在赋值函数中忘了delete原来的内存:(
他们能不能考点新颖的
zsx123 2005-07-14
  • 打赏
  • 举报
回复
明白了,谢了
zsx123 2005-07-14
  • 打赏
  • 举报
回复
cout<<other.m_data;//m_data是other对象的私有数据,怎么可以这样得到呢?请指教。
/* 类的成员函数当然可以访问类的私有数据 */
--------------------------------------------------
但m_data并没有通过other实例的成员函数访问的呀?而是直接用了other.m_data,谢谢指教!!!

像如下的例子:
class string
{
private:
int a;

public:
string()
{
a=1;
}
};

void main()
{
string mystring;
cout<<mystring.a;
}
llf_hust 2005-07-14
  • 打赏
  • 举报
回复
String(const String &other) // 拷贝构造函数
{
cout<<other.m_data; //????????????
int len = strlen(other.m_data);
m_data = new char[len+1];
strcpy(m_data,other.m_data);

}
cout<<other.m_data;//m_data是other对象的私有数据,怎么可以这样得到呢?请指教。
/* 类的成员函数当然可以访问类的私有数据 */
mihonghang 2005-07-14
  • 打赏
  • 举报
回复
晕 这个代码很多吧 ??林锐的 高质量开发 下了 看看吧。
zsx123 2005-07-14
  • 打赏
  • 举报
回复
String(const String &other) // 拷贝构造函数
{
cout<<other.m_data; //????????????
int len = strlen(other.m_data);
m_data = new char[len+1];
strcpy(m_data,other.m_data);

}
cout<<other.m_data;//m_data是other对象的私有数据,怎么可以这样得到呢?请指教。
whatsouta 2005-07-14
  • 打赏
  • 举报
回复
String(const String &other) // 拷贝构造函数
{
cout<<other.m_data; //这里要调用公共方法访问,不能直接访问。
int len = strlen(other.m_data);
m_data = new char[len+1];
strcpy(m_data,other.m_data); //这里要调用公共方法访问,不能直接访问。

}
cout<<other.m_data;//m_data是other对象的私有数据,怎么可以这样得到呢?请指教。
/* 类的成员函数当然可以访问类的私有数据 */


CSDNWW 2005-07-14
  • 打赏
  • 举报
回复
>>String & operate =(const String &other);// 赋值函数
题目都错了, 是operator!!
CSDNWW 2005-07-14
  • 打赏
  • 举报
回复
>>String & operate =(const String &other);// 赋值函数
题目都出错了!应是:
String & operator =(const String &other);// 赋值函数
NoneSoVile 2005-07-14
  • 打赏
  • 举报
回复
全部都是垃圾,异常安全没一个
MagicCarmack 2005-07-13
  • 打赏
  • 举报
回复
林锐 高质量C++
Extreme007 2005-07-13
  • 打赏
  • 举报
回复
关于为什么用string &other 而不用 string other 的区别?

个人认为...因为此类定义的私有成员是一个字符指针涉及到内存地址问题.

因为在把对象当作实参传递时...对象会生成一个临时副本.是实参的一个副本.这个副本的内容与实参一模一样,也就是实参的私有成员*m_data与副本的私有成员*m_data同指向一块内存地址.而副本传递结束时会销毁.相应的实参的*m_data的值也就丢失了.这是问题的关键所在.(实际传递时的是对象的副本)所以此处用的是引用..用引用就会解决这个问题,当实参在传递的过程中不会产生副本的现象.
本人认为(产生对象副本)不太好理解,比较抽象的一个问题...我也是个初学者水平有限.
也是由于这个问题导致C++中的"复制构造函数"的产生
加载更多回复(16)

65,208

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧