none
return overloading in c++ RRS feed

  • Question

  • Starting in myProc1(). I found that myProc2() operates

    on separate Variable on stack than myString1. After there is operator=

    which has to have CONST parameter. 

    Following app breaks up because setting other string to NULL is not possible.

    How to make it working?


    class string {
    public:
        char* _string;
        string() {    }
        string(char* newChars) {
            _string = newChars;
        }
        ~string() {
            delete _string;
        }
        string(string& other) {//made for return declaration
            _string = other._string;
            other._string = NULL;
        }
        string& operator=(const string& other) {
            if (this == &other)
                return *this;
            _string = other._string;
         //   other._string = NULL;//NOT WORKING, HAD TO BE CONST
            return *this;
        }
    };


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

    void myProc1() {
        string myString1;
        myString1 = myProc2();
    }

    Saturday, November 21, 2020 10:50 AM

Answers

All replies