简体   繁体   中英

Operator Overloading Post Decrement

I have to overload post decrement operator in which I have to Copy all elements of set1 into set2. After copying, decrement all elements of set1 (set2 = set1--;). plzz tell me how can I implement this???? The simple post decrement operator I overloaded is as follows:

Set & operator --(int) 
    {
        for(int i=0;i<size;i++)
        {
            arr[i] --;
        }
        return *this;
    }

Your postdecrement operator is wrong, it should not return reference to *this . As you can see here: https://en.cppreference.com/w/cpp/language/operator_incdec its declaration is:

T T::operator--(int);

Post-increment and post-decrement creates a copy of the object, increments or decrements the value of the object and returns the copy from before the increment or decrement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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