我已经创建了一个对象,对于长整数,它的行为有点像无穷大。特别:
#ifndef MU_INF_H
#define MU_INF_H
#include "mu.h"
namespace mu {
class Inf {
public:
bool operator> ( long int i ) { return true; }
bool operator> ( Inf i ) { return false; }
... lots of other boolean operators ...
Inf& operator+ ( long int i ) { return *this; }
Inf& operator+ ( Inf i ) { return *this; }
... lots of other integer operators ...
}; // class Inf
} // namespace mu
#endif
这一切都很好,允许我运行表单的单元测试:
mu::Inf inf;
long int n = -1;
long int z = 0;
long int p = 1;
ASSERT((inf + inf) == inf);
ASSERT((inf + n) == inf);
ASSERT((inf + z) == inf);
ASSERT((inf + p) == inf);
ASSERT((inf > inf) == false);
ASSERT((inf > n) == true);
ASSERT((inf > z) == true);
ASSERT((inf > p) == true);
冒着无法指定复选标记的风险,我有三个问题:
- C ++是否已经提供了这样的东西,和/或是否有比我在这里做的明显更好的方式?
- 我想在整个系统中创建一个Inf实例。我不能宣布它
static const
因为它不是一个“简单”的对象。什么是正确的方法:全球?单身模式?
- 有没有办法处理long int首先出现的对称运算符,即
ASSERT((1 + inf) == inf)
? (如果没有,我不会太伤心。)
static const Inf kInfinity;
工作,将使用默认构造函数。
operator+
应该是一个按值返回的自由函数:
Inf operator+(Inf a, Inf b) { return a += b; }
您表示您希望返回引用 kInfinity
而不是一个值。这是可能的(尽管对我来说似乎有些笨拙);一个 const
因此,必须返回参考 kInfinity
是 const
。
每个人都非常有助于指导我走向真正的道路。为了帮助那些寻找答案的人,而不是让你从整个线程中拼凑出答案,我想我会发布我最终的答案。当然,欢迎提出改进的评论。
#ifndef MU_INDEFINITE_H
#define MU_INDEFINITE_H
namespace mu {
class Indefinite {
public:
static const Indefinite kIndefinite;
Indefinite() {}
~Indefinite() {}
};
inline Indefinite operator+ (long int i, Indefinite t) { return Indefinite::kIndefinite; }
inline Indefinite operator+ (Indefinite t, long int i) { return Indefinite::kIndefinite; }
inline Indefinite operator+ (Indefinite t1, Indefinite t2) { return Indefinite::kIndefinite; }
inline Indefinite operator- (long int i, Indefinite t) { return Indefinite::kIndefinite; }
inline Indefinite operator- (Indefinite t, long int i ) { return Indefinite::kIndefinite; }
inline Indefinite operator- (Indefinite t1, Indefinite t2) { return Indefinite::kIndefinite; }
inline Indefinite operator* (long int i, Indefinite t) { return Indefinite::kIndefinite; }
inline Indefinite operator* (Indefinite t, long int i ) { return Indefinite::kIndefinite; }
inline Indefinite operator* (Indefinite t1, Indefinite t2) { return Indefinite::kIndefinite; }
// It's not clear what i / Indefinite should produce. Forbid it for now.
// inline long int operator/ (long int i, Indefinite t) { return 0; }
inline Indefinite operator/ (Indefinite t, long int i ) { return Indefinite::kIndefinite; }
inline Indefinite operator/ (Indefinite t1, Indefinite t2) { return Indefinite::kIndefinite; }
inline bool operator> (long int i, Indefinite t) { return false; }
inline bool operator> (Indefinite t, long int i) { return true; }
inline bool operator> (Indefinite t1, Indefinite t2) { return false; }
inline bool operator>= (long int i, Indefinite t) { return false; }
inline bool operator>= (Indefinite t, long int i) { return true; }
inline bool operator>= (Indefinite t1, Indefinite t2) { return true; }
inline bool operator< (long int i, Indefinite t) { return true; }
inline bool operator< (Indefinite t, long int i) { return false; }
inline bool operator< (Indefinite t1, Indefinite t2) { return false; }
inline bool operator<= (long int i, Indefinite t) { return true; }
inline bool operator<= (Indefinite t, long int i) { return false; }
inline bool operator<= (Indefinite t1, Indefinite t2) { return true; }
inline bool operator== (long int i, Indefinite t) { return false; }
inline bool operator== (Indefinite t, long int i) { return false; }
inline bool operator== (Indefinite t1, Indefinite t2) { return true; }
inline bool operator!= (long int i, Indefinite t) { return true; }
inline bool operator!= (Indefinite t, long int i) { return true; }
inline bool operator!= (Indefinite t1, Indefinite t2) { return false; }
}
#endif