【问题标题】:operator overloading c++ (x==y==z)运算符重载 c++ (x==y==z)
【发布时间】:2014-12-06 16:55:06
【问题描述】:

我遇到了一个奇怪的问题,我不知道如何在重载中解决。 我也尝试过重载运算符 == 。 简单的例子是:

class bla{
public:
int a;
void bla(int a):a(a){}//costructor
bool operator==(const bla& ob)
     return (a==ob.a);//chk if equal
};
void main(){
bla A,B;  
if (A==B)
   flag=1;//just an example...

这很简单,效果很好,我正在尝试处理以下情况:

   if (A==B==C==D)

所以我需要返回类型对象,现在输入 bool。 我试图添加另一个功能:

bla &bla:: operator==(const bool answer){//as a member function
if (answer)
    return *this;

但它似乎没有帮助。有什么建议吗? 谢谢大家

【问题讨论】:

  • 如果您真的必须拥有此功能(我对此提出质疑),那么您可以让运算符返回一个代理类型,该类型既可用于布尔上下文,也可用于进一步比较。
  • 为什么?更改 C++ 的预期语法规则对您有什么好处?
  • 搞笑,class bla 没有字段x!!!
  • 嗨,@StavSer。如果这确实是一项作业,并且您被要求支持A==B==C==D 语法,则强烈暗示您尚未分享有关此作业的一些关键细节。或者分配这个的人已经疯了。
  • 请注意,您正在更改(或试图更改)C++ 的现有语法。如果您执行 int a、b、c 并执行 if (a == b == c),它将比较 a == b,然后将 c 与结果进行比较,在 99.9% 的情况下,这不是您想要的。所以看到上面代码的人肯定会很困惑,除非到处都说得很清楚就是这样用的……

标签: c++ class overloading


【解决方案1】:

这是一个可怕的想法,你不应该这样做。

这是你的做法。

#include <type_traits>

template<typename T, typename U>
struct decay_equiv
  : std::is_same<
    typename std::decay<T>::type,
    typename std::decay<U>::type
  >::type {};

template<typename T>
struct comparator {
  bool res;
  T passalong;
  explicit operator bool() { return res; }
  typename std::enable_if<
    !decay_equiv<T, comparator<T> >::value,
    comparator<T>
  >::type operator==(T const& rhs);
  comparator<T> operator==(comparator<T> const& rhs);
};

template<typename T>
typename std::enable_if<
  !decay_equiv<T, comparator<T>>::value,
  comparator<T>
>::type comparator<T>::operator==(T const& rhs) {
  if (!res) {
    return {res, rhs};
  }
  return {(passalong == rhs).res, rhs};
}

template<typename T>
comparator<T> comparator<T>::operator==(comparator<T> const& rhs) {
  if (!res || !rhs.res) {
    return {res, rhs};
  }
  return {(passalong == rhs.passalong).res, rhs.passalong};
}

struct bla {
  int a;
  comparator<bla> operator==(bla const& rhs);
  comparator<bla> operator==(comparator<bla> const& rhs);
};

comparator<bla> bla::operator==(bla const& rhs) {
  return {a == rhs.a, rhs};
}

comparator<bla> bla::operator==(comparator<bla> const& rhs) {
  if (!rhs.res) {
    return rhs;
  }
  return {a == rhs.passalong.a, rhs.passalong};
}

int main() {
  bla a = {0},b = {0},d = {0};
  if (a==b==d)
    return 0;
  return -1;
}

此代码假定为 C++11,但只需非常小的更改即可用 C++98 编写。

【讨论】:

  • 谢谢分配,我不知道如何使用模板,但我会查一下。
【解决方案2】:

我设法找到了一种不同的方法,它似乎效果很好。 谢谢大家的帮助和建议。

class bla{
public:
int a;
bool f;
void bla(int a):a(a){f = true;}//costructor
operator bool(){return f};
bla& operator==(const bla &ob){//chk if equal
    if (f)
       f=(a==ob.a);
     return *this;
}
};
void main(){
bla A(4),B(4),C(4),D(4);  
if (A==B==C==D)
   flag=1;//just an example...

【讨论】:

  • 在这种情况下,您可能希望将f 设为私人成员。如果您连续进行两次比较,您的代码也会中断,第一次比较失败。
  • 例如:bla A(1), B(2), C(1); if (A==B==C) int flag = 1; B.a=1; if (A==B==C) int flag=1; 将错误地导致第二次比较失败。
猜你喜欢
  • 2016-06-26
  • 2015-09-06
  • 2016-12-29
  • 1970-01-01
  • 1970-01-01
  • 2013-02-26
  • 2021-04-25
  • 1970-01-01
  • 2011-05-04
相关资源
最近更新 更多