一个简单的类,Release编译模式下的小问题,怀疑和=操作符重载有关!

DDGG DDGG 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
#include "stdafx.h"
#include "stdlib.h"

//////////////// CCellRange 类 ////////////////

class CCellRange
{
public:

CCellRange(int nMinRow = -1, int nMinCol = -1, int nMaxRow = -1, int nMaxCol = -1)
{
Set(nMinRow, nMinCol, nMaxRow, nMaxCol);
}

void Set(int nMinRow = -1, int nMinCol = -1, int nMaxRow = -1, int nMaxCol = -1);

int GetMinRow() const {return m_nMinRow;}
void SetMinRow(int minRow) {m_nMinRow = minRow;}

int GetMinCol() const {return m_nMinCol;}
void SetMinCol(int minCol) {m_nMinCol = minCol;}

int GetMaxRow() const {return m_nMaxRow;}
void SetMaxRow(int maxRow) {m_nMaxRow = maxRow;}

int GetMaxCol() const {return m_nMaxCol;}
void SetMaxCol(int maxCol) {m_nMaxCol = maxCol;}

int GetRowSpan() const {return m_nMaxRow - m_nMinRow + 1;}
int GetColSpan() const {return m_nMaxCol - m_nMinCol + 1;}

void operator=(const CCellRange& rhs);
int operator==(const CCellRange& rhs);
int operator!=(const CCellRange& rhs);

protected:
int m_nMinRow;
int m_nMinCol;
int m_nMaxRow;
int m_nMaxCol;
};

inline void CCellRange::Set(int minRow, int minCol, int maxRow, int maxCol)
{
m_nMinRow = minRow;
m_nMinCol = minCol;
m_nMaxRow = maxRow;
m_nMaxCol = maxCol;
}

inline void CCellRange::operator=(const CCellRange& rhs)
{
if (this != &rhs) Set(rhs.m_nMinRow, rhs.m_nMinCol, rhs.m_nMaxRow, rhs.m_nMaxCol);
}

inline int CCellRange::operator==(const CCellRange& rhs)
{
return ((m_nMinRow == rhs.m_nMinRow) && (m_nMinCol == rhs.m_nMinCol) &&
(m_nMaxRow == rhs.m_nMaxRow) && (m_nMaxCol == rhs.m_nMaxCol));
}

inline int CCellRange::operator!=(const CCellRange& rhs)
{
return !operator==(rhs);
}

//////////////// GetCellRange 函数 ////////////////

CCellRange GetCellRange()
{
CCellRange result(2, 0, -1, -1);

return result;
}

//////////////// 主函数 ////////////////

int main(int argc, char* argv[])
{
CCellRange cellRange;

cellRange = GetCellRange();

// 正确结果应该输出 2,但却输出 -1 (Debug编译时正常)
printf("%d\n", cellRange.GetMinRow());
system("pause");

return 0;
}



开发环境是VC6,Release编译模式。
如果调用时写成 CCellRange cellRange = GetCellRange(); 或者取消CCellRange类中对=操作符的重载也正常。
看了=操作符的代码好像没有问题啊
...全文

相关推荐

评论 22
收藏 已收藏
分享
返回顶部
DDGG
新手乐园 33,314 成员 41,769 内容