它应该复制一个 AnimatedSprite。我有第二个想法,它具有更改 *this 对象的不幸副作用。
我将如何在没有副作用的情况下实现此功能?
编辑:
基于新的答案,问题应该是:如何实现具有公共命名方法且没有副作用的非公共赋值运算符?(因此更改了标题)。
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;
}