Implementing a non-public assignment operator with a public named method?

Posted by Casey on Stack Overflow See other posts from Stack Overflow or by Casey
Published on 2012-07-04T19:20:34Z Indexed on 2012/07/04 21:16 UTC
Read the original article Hit count: 161

Filed under:
|
|

It is supposed to copy an AnimatedSprite. I'm having second thoughts that it has the unfortunate side effect of changing the *this object.

How would I implement this feature without the side effect?

EDIT:

Based on new answers, the question should really be: How do I implement a non-public assignment operator with a public named method without side effects? (Changed title as such).

public:
AnimatedSprite& AnimatedSprite::Clone(const AnimatedSprite& animatedSprite) {
    return (*this = animatedSprite);
}

protected:
AnimatedSprite& AnimatedSprite::operator=(const AnimatedSprite& rhs) {
    if(this == &rhs) return *this;

    destroy_bitmap(this->_frameImage);
    this->_frameImage = create_bitmap(rhs._frameImage->w, rhs._frameImage->h);
    clear_bitmap(this->_frameImage);
    this->_frameDimensions = rhs._frameDimensions;
    this->CalcCenterFrame();
    this->_frameRate = rhs._frameRate;
    if(rhs._animation != nullptr) {
        delete this->_animation;
        this->_animation = new a2de::AnimationHandler(*rhs._animation);
    } else {
        delete this->_animation;
        this->_animation = nullptr;
    }

    return *this;
}

© Stack Overflow or respective owner

Related posts about c++

Related posts about clone