稳定盈利的期货交易方法-量化趋势交易

alantop -专业量化投资者

爱好:量化投资,逆向工程,渗透
随笔 - 595, 文章 - 0, 评论 - 921, 引用 - 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 阅读(308) 评论(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 | 笑笑生

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理