定义一个像整数无穷大一样的对象

时间:2014-05-05 05:14:34

标签: c++

我已经创建了一个对象,其长度有点像无穷大。具体做法是:

#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)? (如果没有,我不会感到太伤心。)

3 个答案:

答案 0 :(得分:6)

  1. 虽然在我看来你在重载中以混乱的方式使用Inf和实际对象的引用,但我并不知道。

    通常,您通过值或const引用获取参数,并为除复合赋值(通过引用返回的位置)之外的所有运算符返回值,以获得预期的语义。当然,由于你的Inf对象没有状态,这一切只在某种程度上才有意义。

  2. 我使用const全局来避免括号和单例中涉及的潜在函数调用。这是否也是static应该几乎没有区别(您无法以任何方式访问this。)

  3. 您必须将您的运算符编写为自由函数:

    inline Inf operator+(long int i, const Inf&) { return *this;} 
    

答案 1 :(得分:5)

static const Inf kInfinity;可以使用默认构造函数。

operator+应该是一个按值返回的自由函数:

Inf operator+(Inf a, Inf b) { return a += b; }

您表示您希望返回对kInfinity的引用而不是值。这是可能的(虽然对我来说似乎有点笨拙);由于constkInfinity,因此必须返回const引用。

答案 2 :(得分:0)

每个人都非常有助于指导我走向真正的道路。为了帮助那些寻找答案的人,而不是让你从整个主题中拼凑出答案,我想我会发布我最终的结果。当然,欢迎提出改进的评​​论。

#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
相关问题