2017-02-03 12 views
0

내 자신의 클래스 문자열을 만들려고합니다. 연산자 오버로딩에 몇 가지 문제가 있습니다.연산자가 자신의 문자열에 대해 오버로드 (+ =, =)

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 (블록).

+0

코드를 디버거와 함께 라인 단위로 isnpecting 할 때 당신은 무엇을 관찰 했습니까? –

+0

"런타임 오류"는 무엇입니까? 그에 따라 질문을 편집하십시오. –

+1

@ aleshka-batman 이러한 연산자를 사용하는 방법을 보여 주어야합니다. 예를 들어 복사 할당 연산자가 분명히 잘못되었습니다. 또한 복사 생성자를 정의해야합니다. –

답변

3

operator+=operator=는 일반적으로 this참조를 돌아갑니다.

현재 컴파일러에서 생성 한 복사본 생성자를 사용하여 값을 반환합니다. 이 생성자는 충돌의 근본 원인 인 데이터 버퍼 포인터 value을 가져옵니다. 포인터에 다중 delete[]이 잘 끝나지 않습니다!

"규칙 3"을 연구하고 복사 생성자와 할당 연산자를 빌드하고 오버로드 된 연산자 반환 유형을 수정 한 다음 계속하십시오.

+0

"3의 규칙"이 나를 위해 일했습니다. 나는 복사 생성자를 정의해야만했다. 제대로 작동합니다. 고마워. –

+0

롯트는 여기에서 할 일이 많습니다. My_string (const char *);을 고려하십시오. 공부하는 데 도움이되는 C++에 관한 좋은 책을 구하십시오. – Bathsheba

0

적어도 복사 할당 연산자는 처음에 객체가 삭제 되었기 때문에 객체가 자신에게 할당 될 때 심각한 버그를 포함합니다. 또한 운영자는 자신에 대한 참조를 반환해야합니다.

는 음, 운영자는 사용자가 명시 적으로도 복사 생성자를 정의 할 필요가 고려 다음과 같은 방법을

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

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

을 정의 할 수 있습니다.