operator return overloading

josh BAUER 166 Reputation points
2020-11-21T08:29:00.193+00:00

I use following code. There is class string that automatically frees memory _string
while returning from procedure. But there is problem when I wish to return string variable
one level up. It both frees memory and returns this.

Is there any way to overload return operator so that It would not free memory while
returning variable one level up?

Can I automatically free memory after being disposed as variable in procedure and
at the same time have possiblity to return variable one level up when needed?

class string {
public:
    char* _string = NULL;
    string() {    }
    string(char* newChars) {
        _string = newChars;
    }
    ~string() {
        free(_string);
    }
    operator char*& () { return _string; }
};


string myProc2() {
    string myString2 = new char[] {'A','B',0};
    return myString2;
}

void myProc1() {
    string myString1;
    myString1=myProc2();
}
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,734 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. RLWA32 45,571 Reputation points
    2020-11-21T11:00:24.953+00:00

    In addition to the point made by @Barry Schwarz about the mismatch between new and free the compiler will use its default copy constructor/copy assignment operator. Consequently, it will perform a shallow copy of the class resulting in various pointers to the allocated memory pointed to by the _string member variable. If you want your class to own the memory allocated to the _string member variable then it needs to do a deep copy -- you must write your own copy constructor/copy assignment functions for the class. Even better, include move constructor/move assignment operator also.

    Simple example -

    class mystring {  
        char* _string = nullptr;  
      
    public:  
        mystring()  
        {  
        }  
          
        mystring(char* newChars)  
        {  
            size_t nChars = strlen(newChars) + 1;  
            _string = new char[nChars];  
            strcpy_s(_string, nChars, newChars);  
        }  
          
        mystring(const mystring& rhs)  
        {  
            if (_string)  
                delete[] _string;  
            size_t nChars = strlen(rhs._string) + 1;  
            _string = new char[nChars];  
            strcpy_s(_string, nChars, rhs._string);  
        }  
      
        mystring(mystring&& rhs)  
        {  
            if (_string)  
                delete[] _string;  
      
            _string = rhs._string;  
            rhs._string = nullptr;  
        }  
      
        mystring& operator=(const mystring& rhs)  
        {  
            if (_string)  
                delete[] _string;  
            size_t nChars = strlen(rhs._string) + 1;  
            _string = new char[nChars];  
            strcpy_s(_string, nChars, rhs._string);  
            return *this;  
        }  
      
        mystring& operator=(mystring&& rhs)  
        {  
            if (_string)  
                delete[] _string;  
      
            _string = rhs._string;  
            rhs._string = nullptr;  
            return *this;  
        }  
      
        ~mystring() {  
            if(_string)  
                delete [] _string;  
        }  
      
        operator char* () { return _string; }  
    };  
      
      
    mystring myProc2() {  
        mystring myString2 = "AB";  
        return myString2;  
    }  
      
    void myProc1() {  
        mystring myString1;  
        myString1 = myProc2();  
    }  
    
    
      
    
    1 person found this answer helpful.

  2. josh BAUER 166 Reputation points
    2020-11-21T08:29:59.267+00:00

    The start of this would be procedure myProc1();
    So:

    myProc1();


  3. josh BAUER 166 Reputation points
    2020-11-21T14:07:43.74+00:00

    mystring(char* newChars) would leek memory in expression like below:

    mystring jjj=new char[]{'A','B',0};

    It would be allocated new memory in constructor and copied deeply.

    Memory allocated by new char[]{'A','B',0} would be never disposed of.

    Better would be to move pointers.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.