string 构造 拷贝 和析构函数
已知String的原型为
class String
{
public:
String(const char *str =NULL);//普通构造函数
String(const String &other);//拷贝构造函数
~String(void);//析构函数
String &operate=(const String &other);//赋值函数
private:
char *m_date;//用于保存字符串
};
0
- 回复
- 1楼
- 2011-03-09 13:03
- 举报 |
- hufeng3405871
- 中级粉丝2/*普通构造函数*/
String(const char *str = NULL)
{
if(str == NULL)
{
m_date=new char[1];
*m_date='/0';
}
else
{
int Long=strlen(str);
this->m_date=new char[Long+1];
strcpy(m_dat,str);
}
}
回复- 2楼
- 2011-03-09 13:10
- 举报 |
- hufeng3405871
- 中级粉丝2/*拷贝构造函数*/
String(const String &other)
{
int Long=strlen(other.m_date);
m_date=new char[Long+1];
strcpy(m_date,other.m_date);
}
回复- 3楼
- 2011-03-09 13:15
- 举报 |
- hufeng3405871
- 中级粉丝2/*构造函数*/
~String(void)
{
delete []m_date;
}
回复- 4楼
- 2011-03-09 13:16
- 举报 |
- hufeng3405871
- 中级粉丝2/*赋值函数*/
String operate(const String &other)
{
if(this == &other)
{
return *this;
delete [] m_date;
}
else
{
int Long=strlen(other.m_date);
m_date=new char[Long+1];
strcpy(m_date,other.m_date);
return *this;
}
}
- 举报 |
- 举报 |
- 举报 |
- 举报 |