【问题标题】:Operators overloading (+=,=) for own strings为自己的字符串重载 (+=,=) 的运算符
【发布时间】:2017-02-03 13:20:31
【问题描述】:

我正在尝试创建自己的类字符串。 我在运算符重载方面遇到了一些问题。

My_string.h

  #include <cstring>
  #include <iostream>
    class My_string
    {
    private:
        char *value;
    public:
        My_string();
        My_string(char *);
        ~My_string();
        My_string operator +=(const My_string&);
        My_string operator =(const My_string&);
        void show()const;
    };

My_string.cpp

#include "stdafx.h"
#include "My_string.h"


My_string::My_string()
{
    value = new char[1];
    strcpy(value, "");
}

My_string::My_string(char * r_argument)
{

    value = new char[strlen(r_argument) + 1];
    strcpy(value, r_argument);
}

My_string::~My_string()
{
    delete[]value;
}

My_string My_string::operator+=(const My_string &r_argument)
{
    char * temp_value = new char[strlen(value) + strlen(r_argument.value) + 1];
    strcpy(temp_value, value);
    strcat(temp_value,r_argument.value);
    delete[]value;
    value = new char[strlen(value) + strlen(r_argument.value) + 1];
    strcpy(value, temp_value);
    delete[]temp_value;
    return *this;
}

void My_string::show() const
{
    std::cout << value << std::endl;
}

My_string My_string::operator =(const My_string & r_argument)
{
    delete[] value;
    value = new char[strlen(r_argument.value)+1];
    strcpy(value, r_argument.value);
    return *this;
}

如何重载 += 和 = 运算符?它们都会导致运行时错误。我需要全部都在动态分配的内存中。

调试断言失败! ... 表达式:_CrtisValidHeapPointer(block)。

【问题讨论】:

  • 使用调试器逐行检查代码时,您观察到了什么?
  • 什么“运行时错误”?请相应地编辑您的问题。
  • @aleshka-batman 您应该展示如何使用这些运算符,例如复制赋值运算符显然是错误的。您还必须定义复制构造函数。
  • 你的操作符 +== 返回 copy 但你没有你的 copy-constructor
  • @Tomáš M 已编辑。

标签: c++ operator-overloading new-operator delete-operator


【解决方案1】:

operator+=operator= 通常将 references 返回到 this

目前您正在按值返回,它使用编译器生成的复制构造函数。该构造函数获取数据缓冲区指针value,这是导致崩溃的根本原因:指针上的多个delete[]s 不会很好地结束!

从研究“规则 3”开始,构建复制构造函数和赋值运算符,修复重载的运算符返回类型,然后从那里继续。

【讨论】:

  • “3 规则”对我有用。我必须定义复制构造函数,仅此而已。它工作正常。非常感谢。
  • 这里还有很多事情要做:例如考虑My_string(const char *);。一定要买一本关于 C++ 的好书来帮助你学习。
【解决方案2】:

至少复制赋值运算符在将对象分配给自身时包含一个严重的错误,因为首先它被删除了。操作员也应该返回对自己的引用。

好吧,操作符可以这样定义

My_string & My_string::operator +=( const My_string &r_argument )
{
    if ( r_argument.value[0] )
    { 
        char *temp_value = new char[strlen(value) + strlen(r_argument.value) + 1];
        strcpy(temp_value, value);
        strcat(temp_value,r_argument.value);

        delete [] value;
        value = temp_value;
    }

    return *this;
}

My_string & My_string::operator =( const My_string &r_argument )
{
    if ( this != &r_argument )
    {
        char *temp_value = value;

        size_t n = strlen( r_argument.value );

        if ( n != strlen( value ) )
        {
            temp_value = new char[ n + 1 ];
        }
        else
        {
            value = nullptr;
        }

        strcpy( temp_value, r_argument.value );

        delete [] value;

        value = temp_value;
    }

    return *this;
}

考虑到您还需要显式定义复制构造函数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多