Question : Perfect assignment operator 1

Hello,

How should i make perfect assignment operator for this class?

class TAAA : public TSuperAAA
{
   TBBB* b1_;
   TBBB* b2_;
};
TBBB is monomorphic and b1_, b2_ point something.

Answer : Perfect assignment operator 1

>> especially when Infinity08: wrote that guarantee is not needed in real system (if i understood).

No, I didn't mean that a guarantee is not needed. What I said in response to ambience's post was that I prefer dealing with error conditions differently than his approach.

There are two approaches here (I've added my own personal thoughts too) :

(a) Try to write the assignment operator to leave the object in a valid state for every possible scenario. This is basically impossible, or extremely hard at the very least. There are too many variables involved, and too many possible error scenario's. Additionaly, this kind of approach adds restrictions to what the code can do, which is not desirable in many cases.

(b) Avoid the added complexity that comes with the previous approach, and instead defer the responsibility to the calling code. The mechanism of exceptions is perfectly suited for that. Furthermore, it is usually the calling code that knows best how to deal with an error (either by trying to fix it, to ignore it, to fail gracefully, etc.), so that's another reason to prefer this approach.

The second approach is what I recommend for the general case, because it's more flexible, and easier to implement, and when done properly, you don't lose anything with regards to code safety.
The first approach is useful in certain specific cases, but you'll quickly get buried in complexities if you try to use it everywhere heh.



>> My inspiration for making this post is

It's a nice article, and it indeed covers many of the pitfalls with assignment operators.


>> I and was wondering what is correct answer for that.

You'll note that the article starts with one of the correct ways to approach an assignment operator. The only difference with the code i posted earlier, is that their assignment operator properly cleans up after itself in case the memory allocation fails.

That's good, but not entirely necessary. The class' destructor is designed to clean up an object, so I prefer to leave that responsibility there, instead of spreading it out all over the class' methods.

Towards the end of the article, different approaches are discussed too.


Going over my earlier code again, I did spot one mistake though. The 'delete b1_;' line should have been followed by a 'b1_ = 0;' line (similarly for b2_). Seems I was typing a bit too fast heh. So, it becomes :
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
TAAA& TAAA::operator=(const TAAA& taaa) {
    TSuperAAA::operator=(taaa);                   // call the assignment operator for the base class
    if (this != &taaa) {                          // check for self assignment
        delete b1_;
        b1_ = 0;
        b1_ = new TBBB(taaa.b1_);
        delete b2_;
        b2_ = 0;
        b2_ = new TBBB(taaa.b2_);
    }
    return *this;
}
Random Solutions  
 
programming4us programming4us