自定义字符串类(C ++) [英] Custom string class (C++)

查看:115
本文介绍了自定义字符串类(C ++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们。
我试图为教育和需要目的编写我自己的C ++ String类。

第一件事是我不知道运算符,这就是为什么我要学习它。
我开始写我的类,但是当我运行它它阻止程序,但不会造成任何崩溃。

请阅读下面的代码,然后再阅读:

  class CString 
{
private:
char * cstr;
public:
CString();
CString(char * str);
CString(CString& str);
〜CString();

operator char *();
operator const char *();
CString operator +(const CString& q)const;
CString operator =(const CString& q);
};

首先我不确定我是否宣布一切正确。我尝试googleing关于它,但所有关于重载的教程解释基本的ideea是非常简单,但没有解释如何和每个事情被调用时。例如在my =运算符中,程序调用CString(CString& str);但我没有ideea为什么。

我还附加了下面的cpp文件:

  CString :: CString()
{
cstr = 0;
}
CString :: CString(char * str)
{
cstr = new char [strlen(str)];
strcpy(cstr,str);
}
CString:CString(CString& q)
{
if(this ==& q)
return;
cstr = new char [strlen(q.cstr)+1];
strcpy(cstr,q.cstr);
}
CString ::〜CString()
{
if(cstr)
delete [] cstr;
}
CString :: operator char *()
{
return cstr;
}
CString :: operator const char *()
{
return cstr;
}
CString CString :: operator +(const CString& q)const
{
CString s;
s.cstr = new char [strlen(cstr)+ strlen(q.cstr)+1];
strcpy(s.cstr,cstr);
strcat(s.cstr,q.cstr);
return s;
}
CString CString :: operator =(const CString& q)
{
if(this!=& q)
{
if (cstr)
delete [] cstr;
cstr = new char [strlen(q.cstr)+1];
strcpy(cstr,q.cstr);
}
return * this;
}

对于测试,我使用的代码就像这样简单

CString a = CString(Hello)+ CString(World);

printf(a);

我试过调试它,但在一个点我迷路了。首先,它为hello和world调用构造函数2次。然后它在+运算符,这是罚款。然后它调用空字符串的构造函数。之后它进入CString(CString& str),现在我迷路了。为什么会发生这种情况?之后,我注意到我的字符串包含Hello World是在析构函数(连续几次)。再次,我很迷惑。从char *再转换成Cstring和来回,它停止。它永远不会进入=运算符,但也不会进一步。

我使用VisualStudio 2010,但它基本上只是标准的c ++代码,因此我不认为它应该有很大的区别

解决方案

行:

  cstr = new char [ strlen(str)]; 

应为:

  cstr = new char [strlen(str)+ 1]; 

此外,自我分配的测试在复制构造函数中没有意义 - 新对象 - 它不可能具有与任何现有对象相同的地址。并且复制构造函数应该使用const引用作为参数,



如果在你的代码中,你期望使用赋值运算符,你会期望错误。此代码:

  CString a = CString(Hello)+ CString(World); 

基本上与以下相同:

  CString a(CString(Hello)+ CString(World)); 

这是复制构造,



基本上,它听起来好像你的代码是或多或少的工作。预期。


Hey guys. I'm trying to write my own C++ String class for educational and need purposes.
The first thing is that I don't know that much about operators and that's why I want to learn them. I started writing my class but when I run it it blocks the program but does not do any crash.
Take a look at the following code please before reading further:

class CString
{
private:
  char* cstr;
public:
  CString();
  CString(char* str);
  CString(CString& str);
  ~CString();

  operator char*();
  operator const char*();
  CString operator+(const CString& q)const;
     CString operator=(const CString& q);
};

First of all I'm not so sure I declared everything right. I tried googleing about it but all the tutorials about overloading explain the basic ideea which is very simple but lack to explain how and when each thing is called. For instance in my = operator the program calls CString(CString& str); but I have no ideea why.
I have also attached the cpp file below:

CString::CString()
{
 cstr=0;
}
CString::CString(char *str)
{
 cstr=new char[strlen(str)];
 strcpy(cstr,str);
}
CString::CString(CString& q)
{
 if(this==&q)
  return;
 cstr = new char[strlen(q.cstr)+1];
 strcpy(cstr,q.cstr);
}
CString::~CString()
{
 if(cstr)
  delete[] cstr;
}
CString::operator char*()
{
 return cstr;
}
CString::operator const char* ()
{
 return cstr;
}
CString CString::operator +(const CString &q) const
{
 CString s;
 s.cstr = new char[strlen(cstr)+strlen(q.cstr)+1];
 strcpy(s.cstr,cstr);
 strcat(s.cstr,q.cstr);
 return s;
}
CString CString::operator =(const CString &q)
{
 if(this!=&q)
 {
  if(cstr)
   delete[] cstr;
  cstr = new char[strlen(q.cstr)+1];
  strcpy(cstr,q.cstr);
 }
 return *this;
}

For testing I used a code just as simple as this
CString a = CString("Hello") + CString(" World");
printf(a);
I tried debugging it but at a point I get lost. First it calls the constructor 2 times for "hello" and for " world". Then it get's in the + operator which is fine. Then it calls the constructor for the empty string. After that it get's into "CString(CString& str)" and now I'm lost. Why is this happening? After this I noticed my string containing "Hello World" is in the destructor (a few times in a row). Again I'm very puzzeled. After converting again from char* to Cstring and back and forth it stops. It never get's into the = operator but neither does it go further. printf(a) is never reached.
I use VisualStudio 2010 for this but it's basically just standard c++ code and thus I don't think it should make that much of a difference

解决方案

The line:

cstr=new char[strlen(str)];

should be:

cstr=new char[strlen(str) + 1];

Also, the test for self-assignment does not make sense in the copy constructor - you are creating a new object - it cannot possibly have the same address as any existing object. And the copy constructor should take a const reference as a parameter,

If in your code, you were expecting the assignment operator to be used, you would be expectining wrong. This code:

CString a = CString("Hello") + CString(" World");

is essentially the same as:

CString a( CString("Hello") + CString(" World") );

which is copy construction, not assignment. The temporary CString "Hello world" will be destroyed (invoking the destructor) after a has been constructed.

Basically, it sounds as if your code is working more or less as expected.

这篇关于自定义字符串类(C ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆