我很好奇为什么复制构造函数对我自己定义的类的动态分配如此重要.
我正在使用动态分配实现低级c-string类,这里是我的类的快速视图
class String
{
private:
char * buf;
bool inBounds( int i )
{
return i >= 0 && i < strlen(buf);
}
static int strlen(const char *src)
{
int count = 0;
while (*(src+count))
++count;
return count;
}
static char *strcpy(char *dest, const char *src)
{
char *p = dest;
while( (*p++ = *src++));
return dest;
}
static char* strdup(const char *src)
{
char * res = new_char_array(strlen(src)+1);
strcpy(res,src);
return res;
}
static char * new_char_array(int n_bytes)
{ …Run Code Online (Sandbox Code Playgroud) 感谢你们。我发现我无法在互联网上发布我的问题。
不过,这对我确实很有帮助。