Handle assignment to self in operator=

智能指針

版本一, swap and copy

#include <string>
#include <memory>
#include <iostream>

class Bitmap
{
public:
    Bitmap(int w = 1, int h = 1) :m_width(w), m_height(h){}
    int getWidth() const {
        return m_width;
    }
    int getHeight() const {
        return m_height;
    }
    void setWidth(int w) {
        m_width = w;
    }
    void setHeight(int h) {
        m_height = h;
    }
private:
    int m_width;
    int m_height;
};

class Widget
{
public:
    Widget(){
        m_bp = std::make_shared<Bitmap>(2,2);
    }
    ~Widget() {}
    Widget(const Widget &rhs) {
        m_bp = std::make_shared<Bitmap>(rhs.m_bp->getWidth(), rhs.m_bp->getHeight());
    }
    void swap(Widget &rhs) {
        m_bp.swap(rhs.m_bp);
    }

    /* swap and copy的方式實現自我賦值安全性的copy assignment */
    Widget &operator=(const Widget & rhs){
        if (this->m_bp == rhs.m_bp) { // identity test
            return *this;
        }
        Widget tmp(rhs);
        swap(tmp);
        return *this;
    }

    void setBP(int new_w, int new_h) {
        m_bp->setWidth(new_w);
        m_bp->setHeight(new_h);
    }
private:
    std::shared_ptr<Bitmap> m_bp;
};

int main() {
    Widget aw;
    Widget bw;
    bw.setBP(15, 15);
    aw = bw;
    aw = aw;

    return 0;
}

版本二

#include <string>
#include <memory>
#include <iostream>

class Bitmap
{
public:
    Bitmap(int w = 1, int h = 1) :m_width(w), m_height(h) {}
    int getWidth() const {
        return m_width;
    }
    int getHeight() const {
        return m_height;
    }
    void setWidth(int w) {
        m_width = w;
    }
    void setHeight(int h) {
        m_height = h;
    }
private:
    int m_width;
    int m_height;
};

class Widget
{
public:
    Widget() {
        m_bp = std::make_shared<Bitmap>(2, 2);
    }
    ~Widget() {}
    Widget(const Widget &rhs) {
        m_bp = std::make_shared<Bitmap>(rhs.m_bp->getWidth(), rhs.m_bp->getHeight());
    }

    Widget &operator=(const Widget & rhs) {
        if (this->m_bp == rhs.m_bp) { // identity test
            return *this;
        }
        m_bp.reset(new Bitmap(rhs.m_bp->getWidth(), rhs.m_bp->getHeight()));
        return *this;
    }

    void setBP(int new_w, int new_h) {
        m_bp->setWidth(new_w);
        m_bp->setHeight(new_h);
    }
private:
    std::shared_ptr<Bitmap> m_bp;
};

int main() {
    Widget aw;
    Widget bw;
    bw.setBP(15, 15);
    aw = bw;
    aw = aw;

    return 0;
}

指針

版本一, swap and copy

#include <string>
#include <memory>
#include <iostream>

class Bitmap
{
public:
    Bitmap(int w = 1, int h = 1) :m_width(w), m_height(h) {}
    int getWidth() const {
        return m_width;
    }
    int getHeight() const {
        return m_height;
    }
    void setWidth(int w) {
        m_width = w;
    }
    void setHeight(int h) {
        m_height = h;
    }
private:
    int m_width;
    int m_height;
};

class Widget
{
public:
    Widget() {
        m_bp = new Bitmap(2, 2);
    }
    ~Widget() {}
    Widget(const Widget &rhs) {
        m_bp = new Bitmap(rhs.m_bp->getWidth(), rhs.m_bp->getHeight());
    }
    void swap(Widget &rhs) {
        Bitmap *tmp = m_bp;
        m_bp = rhs.m_bp;
        rhs.m_bp = tmp;
    }

    /* swap and copy的方式實現自我賦值安全性的copy assignment */
    Widget &operator=(const Widget & rhs) {
        if (this->m_bp == rhs.m_bp) { // identity test
            return *this;
        }
        Widget tmp(rhs);
        swap(tmp);
        return *this;
    }

    void setBP(int new_w, int new_h) {
        m_bp->setWidth(new_w);
        m_bp->setHeight(new_h);
    }
private:
    Bitmap *m_bp;
};

int main() {
    Widget aw;
    Widget bw;
    bw.setBP(15, 15);
    aw = bw;
    aw = aw;

    return 0;
}

版本二

#include <string>
#include <memory>
#include <iostream>

class Bitmap
{
public:
    Bitmap(int w = 1, int h = 1) :m_width(w), m_height(h) {}
    int getWidth() const {
        return m_width;
    }
    int getHeight() const {
        return m_height;
    }
    void setWidth(int w) {
        m_width = w;
    }
    void setHeight(int h) {
        m_height = h;
    }
private:
    int m_width;
    int m_height;
};

class Widget
{
public:
    Widget() {
        m_bp = new Bitmap(2, 2);
    }
    ~Widget() {
        delete m_bp;
    }

    Widget &operator=(const Widget & rhs) {
        if (this->m_bp == rhs.m_bp) { // identity test
            return *this;
        }
        Bitmap *tmp = m_bp;
        m_bp = new Bitmap(rhs.m_bp->getWidth(), rhs.m_bp->getHeight());
        delete tmp;

        return *this;
    }

    void setBP(int new_w, int new_h) {
        m_bp->setWidth(new_w);
        m_bp->setHeight(new_h);
    }
private:
    Bitmap *m_bp;
};

int main() {
    Widget aw;
    Widget bw;
    bw.setBP(15, 15);
    aw = bw;
    aw = aw;

    return 0;
}

Reference

Scott Meyers. Effective C++ , 55 Specific Ways to Improve Your Programs and Designs, Third Edition

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章