C++重載運算符

本文主要介紹:結構體中重載運算符、類重載運算符以及容器排序重載運算符。
1、結構體重載運算符

typedef struct tagSaveImgFile
{
    tagSaveImgFile &operator = (tagSaveImgFile &other) //放在結構體內部
    {
        m_scale = other.m_scale;
        m_imgPath = other.m_imgPath;
        m_labelPath = other.m_labelPath;
        m_xmlPath = other.m_xmlPath;

        return *this;

    };

    bool operator <(const tagNeighbor &other )
    {

        return m_dissimCrit<other.m_dissimCrit;
    }

    int m_scale;
    string m_imgPath;
    string m_labelPath;
    string m_xmlPath;
}CSaveFiles;

2、類定義重載運算符

.h文件中類定義:

class CParameter
{
public:
    CParameter();
    ~CParameter();

    CParameter & operator = (const CParameter &other);
    int m_scale;
    string m_imgPath;
    string m_labelPath;
    string m_xmlPath;
};

.cpp中文件中程序:

    CParameter::CParameter()
    {

    }
    CParameter::~CParameter()
    {

    }

    CParameter & CParameter::operator = (const CParameter &other)
    {
        if (this != &other)
        {
            m_scale = other.m_scale;
            m_imgPath = other.m_imgPath;
            m_labelPath = other.m_labelPath;
            m_xmlPath = other.m_xmlPath;
        }
        return *this;
    }

class CNeighbor
{
public:
    int     m_id;
    double  m_dissimCrit;
    double  m_mergedPerim;

    //重載等於
    CNeighbor &operator = (const CNeighbor &other)
    {
        m_id = other.m_dissimCrit;
        m_dissimCrit = other.m_dissimCrit;
        m_mergedPerim = other.m_mergedPerim;
    }
    //重載 < 設定排序順序
    bool operator < (const CNeighbor &other) const //set容器要加const
    {
        return m_dissimCrit < other.m_dissimCrit;
    }

    //構造函數 支持CNeighbor(a,b,c)賦值
    CNeighbor(int id, double dissimCrit, double mergedPerim)
    {
        m_id = id;
        m_dissimCrit = dissimCrit;
        m_mergedPerim = mergedPerim;
    }
};

3、容器排序重載運算符
.h文件定義及重載:

class CParameter
{
public:
    CParameter();
    ~CParameter();

    CParameter & operator = (const CParameter &other);
    int m_scale;
    string m_imgPath;
    string m_labelPath;
    string m_xmlPath;
};

typedef list<CParameter> listParameter;

//list可對存儲的對象排序,重載按m_scale升序排列,即由小到大排序
inline bool operator <(const CParameter &one, const CParameter &other)
{
    return one.m_scale<other.m_scale;
}

.cpp中調用:

        listParameter listpara;

        CParameter para;
        para.m_scale = 300;
        para.m_imgPath = "C:\\Users\\hong\\Desktop\\ObjectSeg100.tif";
        para.m_labelPath = "C:\\Users\\hong\\Desktop\\ObjectLabel100.tif";
        para.m_xmlPath = "C:\\Users\\hong\\Desktop\\ObjectSeg.xml";

        listpara.push_back(para);

        CParameter other;
        other.m_scale = 200;
        other.m_imgPath = "C:\\Users\\hong\\Desktop\\ObjectSeg100.tif";
        other.m_labelPath = "C:\\Users\\hong\\Desktop\\ObjectLabel100.tif";
        other.m_xmlPath = "C:\\Users\\hong\\Desktop\\ObjectSeg.xml";

        listpara.push_back(other);


        cout<<listpara.begin()->m_scale<<endl; //輸出結果爲300

        listpara.sort();

        cout<<listpara.begin()->m_scale<<endl; //輸出結果爲200

注:
1.引用符&的介紹:
http://www.cnblogs.com/Mr-xu/archive/2012/08/07/2626973.html

2.引用與指針區別:
http://xinklabi.iteye.com/blog/653643

3.值傳遞、指針傳遞和引用傳遞效率比較
http://blog.sina.com.cn/s/blog_a3daf09d01015c0r.html

4.const用法:
http://blog.csdn.net/zcf1002797280/article/details/7816977

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