Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/124.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 运算符重载c++;(x==y==z)_C++_Class_Overloading - Fatal编程技术网

C++ 运算符重载c++;(x==y==z)

C++ 运算符重载c++;(x==y==z),c++,class,overloading,C++,Class,Overloading,我遇到了一个奇怪的问题,我不知道如何解决重载。 我正在尝试过重载运算符==。 简单的例子是: 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... 这

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

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)
所以我需要返回object类型,现在返回bool类型。 我已尝试添加另一个函数:

bla &bla:: operator==(const bool answer){//as a member function
if (answer)
    return *this;
但这似乎没有帮助。你有什么建议吗?
thx Stas

这是一个可怕的想法,你永远不应该这么做

这是你怎么做的

#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;
}
#包括
模板
结构衰变
:std::是否相同<
typename std::Decation::type,
typename std::decation::type
>::类型{};
模板
结构比较器{
布尔雷斯;
帕萨朗;
显式运算符bool(){return res;}
typename std::启用\u如果<
!decation_equiv::value,
比较器
>::类型运算符==(T常量和rhs);
比较器运算符==(比较器常量和rhs);
};
模板
typename std::启用\u如果<
!衰减等效值:,
比较器
>::类型比较器::运算符==(T常量和rhs){
如果(!res){
返回{res,rhs};
}
返回{(passalong==rhs).res,rhs};
}
模板
比较器比较器::运算符==(比较器常量和rhs){
如果(!res | |!rhs.res){
返回{res,rhs};
}
返回{(passalong==rhs.passalong.res,rhs.passalong};
}
结构bla{
INTA;
比较器运算符==(bla常量和rhs);
比较器运算符==(比较器常量和rhs);
};
比较器bla::运算符==(bla常量和rhs){
返回{a==rhs.a,rhs};
}
比较器bla::运算符==(比较器常量和rhs){
如果(!rhs.res){
返回rhs;
}
返回{a==rhs.passalong.a,rhs.passalong};
}
int main(){
blaa={0},b={0},d={0};
如果(a==b==d)
返回0;
返回-1;
}

这段代码假定为C++11,但可以用C++98编写,只需做很小的修改。

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

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...

如果您真的必须拥有这个特性(我对此提出了质疑),那么您可以让操作符返回一个代理类型,该类型在布尔上下文和进一步比较中都可用。为什么?更改C++的预期语法规则会有什么好处?搞笑,<代码>类BLA < /代码>没有字段<代码> x<代码>!!!嗨,斯塔夫瑟。如果这确实是一个赋值,并且要求您支持
A==B==C==D
语法,那么这就强烈地暗示了关于这个赋值的一些关键细节您还没有分享。或者指定这个人的想法已经过时了。注意你正在改变(或者试图改变)C++的现有语法。如果你输入a,b,c,如果(a==b==c),它将比较
a==b
,然后将
c
与结果进行比较,99.9%的情况下,结果不是你想要的。因此,看到上述代码的人肯定会感到非常困惑,除非到处都清楚地表明这是它的使用方式…Thx ALLOCT我不知道如何使用模板,但我会查找它。在这种情况下,您可能希望使
f
成为私人成员。如果您在一行中进行两次比较,那么代码也会中断,第一次比较失败;如果(A==B==C)int flag=1;B.a=1;如果(A==B==C)int flag=1将错误地使第二次比较失败。