一个简单的类,Release编译模式下的小问题,怀疑和=操作符重载有关!
#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类中对=操作符的重载也正常。
看了=操作符的代码好像没有问题啊
...全文
相关推荐
javabeginner-release.pdf
构造方法重载(Constructor Overloading):在同一个类中,可以有多个构造方法,它们具有相同的名称但参数列表不同,这种现象称为构造方法重载,根据传入的参数来决定调用哪个构造方法。
VC中常见问题
**Release模式编译错误**:如果网络程序在Debug模式下运行正常,但在Release模式下报错`error LNK2001: unresolved external symbol __imp_
C++期末复习题库.pdf
指针和引用 - “Point*ptr=new Point[2]”和“delete[]ptr;”演示了如何使用new操作符动态分配内存,并使用delete操作符释放内存。
c和c++经典面试题
而宏是预处理器指令,它进行简单的文本替换,没有类型安全,可能导致边界效应和类型不匹配的问题。宏定义的常量在编译时无法进行调试,而枚举常量可以在某些集成开发环境中调试。
史上最详尽的JsonCpp使用指南(含c++封装的源代码)
- **操作符重载**:支持基本的数据类型操作。##### 3.2 Writer 类- **派生类**:JsonCpp 提供了三种 Writer 的派生类来适应不同的输出需求。


新手乐园
