简体   繁体   English

运算符重载(+ =,=)自己的字符串

[英]Operators overloading (+=,=) for own strings

I'm trying to create my own class string. 我正在尝试创建自己的类字符串。 I've got some issues with operator overloading. 我在运算符重载方面遇到了一些问题。

My_string.h 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 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;
}

How to overload += and = operators ?They both causes runtime errors. 如何重载+ =和=运算符?它们均会导致运行时错误。 I need all to be in dynamically allocated memory. 我需要所有人都处于动态分配的内存中。

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

operator+= and operator= normally return references to this . operator+=operator=通常返回this 引用

Currently you are returning by value, which is using the compiler-generated copy constructor. 当前,您正在使用编译器生成的副本构造函数按值返回。 That constructor grabs the data buffer pointer value which is the root cause of your crash: multiple delete[] s on a pointer will not end well! 该构造函数获取数据缓冲区指针value ,这是导致崩溃的根本原因:指针上的多个delete[]不会很好地结束!

Start by researching the "Rule Of 3", build your copy constructors and assignment operators, fix the overloaded operator return types, and continue from there. 首先研究“ 3条规则”,构建副本构造函数和赋值运算符,修复重载的运算符返回类型,然后从那里继续。

At least the copy assignment operator contains a serious bug when an object is assigned to itself because at first it is deleted. 至少当对象分配给自己时,复制分配操作员包含一个严重的错误,因为首先将其删除。 Also the operators should return reference to themselves. 另外,操作员应返回自己的参考。

Well, the operators can be defined the following way 好了,可以通过以下方式定义运算符

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;
}

and

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;
}

Take into account that you need to define explicitly also the copy constructor. 考虑到还需要明确定义副本构造函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM