操作符'=' 重定义?

x363635334 2013-03-05 01:32:44

//this is String.cpp
#include <windows.h>
class String
{
private:
char * m_pCh;
int len;
static int num_strings;//number of string
static const int CINLIM=80;//cin input limit
public:
String();
String(const char * s);
String(const String &s);
int Length()const{return len;};
String & operator=(const String &s);
String & operator=(const char* s);
char & operator[](int i);
const char & operator[](int i)const;
friend bool operator<(const String &s1,const String & s2);
friend bool operator>(const String & s1,const String & s2);
friend bool operator==(const String & s1,const String & s2);
static int HowMany();
};

String & String::operator=(const char *s)
{
delete[] m_pCh;
len = strlen(s);
m_pCh=new char[len+1];
strcpy(m_pCh,s);
return *this;
}

main.cpp里仅包含String.cpp,构建了一个String对象
...全文
124 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
x363635334 2013-03-05
  • 打赏
  • 举报
回复
我这是很小的一个类,如何才能把整个类的声明定义实现放到同一个文件中? 百度搜hpp也没能看懂啊
x363635334 2013-03-05
  • 打赏
  • 举报
回复
等号的实现就是在cpp中阿
palm008 2013-03-05
  • 打赏
  • 举报
回复
请问楼上,放在cpp和放在.h中有什么区别呢?
  • 打赏
  • 举报
回复
String & String::operator=(const char *s)
{
    delete[] m_pCh;
    len  = strlen(s);
    m_pCh=new char[len+1];
    strcpy(m_pCh,s);
    return *this;
}
放到cpp中,不要放在头文件中

33,322

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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