本文介绍了T :: operator int()const与T :: operator Handle()const不明确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 请照亮;什么类'事件''的运算符将匹配这些 两种情况[见后面的事件大纲]: 事件ev1; 活动ev2; //案例1 // if(ev1) ; //案例2 // if(ev1 || ev2) ; 我原以为''operator int()''是唯一明显的匹配,但是我的 编译器会产生错误: "布尔的''struct Event''转换的模糊3向选择 背景 全部我正在尝试将一个OS事件包装在一个可以被视为 a布尔的类中。任何人都可以解释这种歧义是什么以及如何解决这个问题(最好没有演员阵容)? 祝你好运 Tim typedef struct tagHandle {} *句柄; bool eventIsSignalled(Handle hEvent); struct Event { : bool IsSignalled()const {return eventIsSignalled(* this); } $ /> 运算符int()const {return IsSignalled(); } $ / $ 运算符Handle()const {return handle; } 私人: 处理句柄; }; 解决方案 ints *和*指针可以转换为boolean(具有相同的优先级)。所以它是暧昧的。 Shezan Baig总结了模糊性。至于做什么,我会定义 ''Event :: operator bool()const''。 Kanenas Please illuminate; what operator of class ''Event'' will get matched for thesetwo cases [see Event outline later]: Event ev1;Event ev2; // Case 1//if (ev1); // Case 2//if (ev1 || ev2); I would have thought ''operator int ()'' is the only obvious match, but mycompiler generates errors: "ambiguous 3-way choice of conversion from ''struct Event'' in Booleancontext All I''m trying to do is wrap an OS event in a class that can be treated likea Boolean; can anyone explain what the ambiguity is and how it can beresolved (preferable without casts)? Best regardsTim typedef struct tagHandle { }* Handle; bool eventIsSignalled(Handle hEvent); struct Event{:bool IsSignalled() const { return eventIsSignalled(*this); } operator int () const { return IsSignalled(); }operator Handle () const { return handle; } private:Handle handle;}; 解决方案 ints *and* pointers are convertible to boolean (with the same priority). So it is ambigious. Shezan Baig summed up the ambiguity. As for what to do, I''d define''Event::operator bool() const''. Kanenas 这篇关于T :: operator int()const与T :: operator Handle()const不明确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-10 08:06