股票数据格式 外汇交易系统 MT4编程 电子海图开发(S52 S57) AIS 电子海图和gis开发qq群:40968136

http://alantop.5166.info 电子海图和gis开发qq群:40968136

股票数据格式 外汇交易系统 MT4编程 电子海图开发(S52 S57) AIS
随笔 - 260, 文章 - 1, 评论 - 468, 引用 - 0
数据加载中……

通过user_string类,来看如何撰写构造函数。

#include <string.h>
#include <stdio.h>

class user_string
{
public :
 user_string();
 user_string(const char *src);
 user_string(const user_string &other);
 user_string &operator = (const user_string &other);
 
 ~user_string();
 void show();
private:
 char *m_pdata;
};

user_string::user_string()
{
}

user_string::user_string(const user_string &other)
{

}

user_string::user_string(const char*src)
{
 if ( src == NULL )
 {
  m_pdata = new char[1];
  m_pdata[0] = '\0';
 }
 else
  m_pdata = new char [strlen(src) + 1];
  strcpy(m_pdata, src);
}

user_string::~user_string()
{
 delete []m_pdata;
}

user_string & user_string::operator = (const user_string &other)
{
 if ( this == &other )
  return *this;
 
 delete []m_pdata;

 m_pdata = new char[strlen(other.m_pdata) + 1];
 strcpy(m_pdata, other.m_pdata);
 return * this;
}


void user_string::show()
{
 printf("%s", m_pdata);
}

void main()
{
 user_string a("123");
 user_string b("456");
 b = b;
 b = a;
 //b = a;
 
}

posted on 2006-06-03 12:06 AlanTop 阅读(184) 评论(2)  编辑 收藏 引用 所属分类: C++

评论

# re: 通过user_string类,来看如何撰写构造函数。  回复  更多评论   

user_string & user_string::operator = (const user_string &other)
{
if ( m_pdata == other ) //这里错了。
return *this;

delete []m_pdata;

m_pdata = new char[strlen(other.m_pdata) + 1];
strcpy(m_pdata, other.m_pdata);
return * this;
}
2006-06-03 13:15 | 任我行

# re: 通过user_string类,来看如何撰写构造函数。  回复  更多评论   

if ( m_pdata == other ) //这里错了
2006-06-03 13:32 | 笑笑生

标题  
姓名  
主页
验证码 *
内容(提交失败后,可以通过“恢复上次提交”恢复刚刚提交的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
[使用Ctrl+Enter键可以直接提交]
相关链接:
网站导航:




股票数据格式 外汇交易系统 MT4编程 电子海图开发(S52 S57) AIS 电子海图和gis开发qq群:40968136