String类的实现 -- C++经典面试题
来源:互联网 发布:java base64编码解码 编辑:程序博客网 时间:2024/06/05 06:39
已知类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::String(const char *str)
{
if(str==NULL)
{
m_data = new char[1]; // 得分点:对空字符串自动申请存放结束标志'\0'的//加分点:对m_data加NULL 判断
*m_data = '\0';
}
else
{
int length = strlen(str);
m_data = new char[length+1]; // 若能加 NULL 判断则更好
strcpy(m_data, str);
}
}
// String的析构函数
String::~String(void)
{
delete [] m_data; // 或delete m_data;
}
//拷贝构造函数
String::String(const String &other) // 得分点:输入参数为const型
{
int length = strlen(other.m_data);
m_data = new char[length+1]; //加分点:对m_data加NULL 判断
strcpy(m_data, other.m_data);
}
//赋值函数
String & String::operate =(const String &other) // 得分点:输入参数为const型
{
if(this == &other) //得分点:检查自赋值
return *this;
delete [] m_data; //得分点:释放原有的内存资源
int length = strlen( other.m_data );
m_data = new char[length+1]; //加分点:对m_data加NULL 判断
strcpy( m_data, other.m_data );
return *this; //得分点:返回本对象的引用
}
剖析:
能够准确无误地编写出String类的构造函数、拷贝构造函数、赋值函数和析构函数的面试者至少已经具备了C++基本功的60%以上!
在这个类中包括了指针类成员变量m_data,当类中包括指针类成员变量时,一定要重载其拷贝构造函数、赋值函数和析构函数,
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个函数。
String::String(const char *str)
{
if(str==NULL)
{
m_data = new char[1]; // 得分点:对空字符串自动申请存放结束标志'\0'的//加分点:对m_data加NULL 判断
*m_data = '\0';
}
else
{
int length = strlen(str);
m_data = new char[length+1]; // 若能加 NULL 判断则更好
strcpy(m_data, str);
}
}
// String的析构函数
String::~String(void)
{
delete [] m_data; // 或delete m_data;
}
//拷贝构造函数
String::String(const String &other) // 得分点:输入参数为const型
{
int length = strlen(other.m_data);
m_data = new char[length+1]; //加分点:对m_data加NULL 判断
strcpy(m_data, other.m_data);
}
//赋值函数
String & String::operate =(const String &other) // 得分点:输入参数为const型
{
if(this == &other) //得分点:检查自赋值
return *this;
delete [] m_data; //得分点:释放原有的内存资源
int length = strlen( other.m_data );
m_data = new char[length+1]; //加分点:对m_data加NULL 判断
strcpy( m_data, other.m_data );
return *this; //得分点:返回本对象的引用
}
剖析:
能够准确无误地编写出String类的构造函数、拷贝构造函数、赋值函数和析构函数的面试者至少已经具备了C++基本功的60%以上!
在这个类中包括了指针类成员变量m_data,当类中包括指针类成员变量时,一定要重载其拷贝构造函数、赋值函数和析构函数,
这既是对C++程序员的基本要求,也是《Effective C++》中特别强调的条款。
阅读全文
0 0
- String类的实现 -- C++经典面试题
- [经典面试题][百度]c++实现STL中的string类
- C++面试题:String类的实现
- C++面试题--string类的实现
- C++面试题:String类的实现
- C++面试题:String类的实现
- 10有关String的经典面试题
- C++经典面试题之---String类
- C++经典面试题之---String类
- C++经典面试题之---String类
- C++经典面试题之---String类
- 类 String理解和经典面试题,
- 经典C面试题
- C++【常见面试题】String类的实现,以及深拷贝、浅拷贝问题
- c string 面试题
- C/C++的指针经典面试题
- C语言的经典面试题
- C++:【常见面试题】String类的写法
- 解决 3ds Max 载入模型后出现 Missing Dlls 问题
- Docker的使用
- 常用正则表达式
- mysql触发器的学习以及使用
- Redis常用的五种数据类型
- String类的实现 -- C++经典面试题
- UI01-第一个UI程序
- Java开发中的23种设计模式详解_1
- 【Python】【Shell】【Caffe】训练集预处理 —— 数据增强 《很认真的讲讲Caffe》
- android 壁纸居中显示
- java简单实现一个日志工具类(用于服务器调试)
- no session found for current thread异常
- [bzoj3566][SHOI2014]概率充电器 树形DP+期望DP
- android连接mysql数据库