T :: operator int()const与T :: operator Handle()const不明确? [英] T::operator int () const ambiguous with 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; }
私人:
处理句柄;
};
Tim Clacy写道:typedef struct tagHandle {} *句柄;
bool eventIsSignalled(Handle hEvent);
struct Event
{
:
bool IsSignalled()const {return eventIsSignalled(* this); }}运算符int()const {return IsSignalled(); }
运算符Handle()const {return handle; }
ints *和*指针可转换为布尔值(具有相同的
优先级)。所以它是暧昧的。
私人:
句柄;
};
Shezan Baig写道:Tim Clacy写道:typedef struct tagHandle {} * Handle;
尝试改为:
struct Handle {SomeType * realHandle; };
bool eventIsSignalled(处理hEvent);
struct事件
{
:
bool IsSignalled()const {return eventIsSignalled( *这个); }}运算符int()const {return IsSignalled(); }
运算符Handle()const {return handle; }
ints *和*指针可以转换为boolean(具有相同的优先级)。所以它是暧昧的。
2005年4月29日星期五18:45:08 +0200,Tim Clacy>
< no ******* @ nospamphaseone.nospamdk>写道:
请照亮;两个案例''事件''的哪个运算符将匹配这两个案例[见后面的事件大纲]:
[...] if(ev1 || ev2)
;
我原以为''operator int()''是唯一明显的匹配,但我的
编译器会产生错误:
" ambiguous 3在布尔
上下文中从''struct Event''转换的选择
我想要做的就是将一个OS事件包装在一个可以被视为
Shezan Baig总结了模糊性。至于做什么,我会定义
''Event :: operator bool()const''。
Kanenas
Please illuminate; what operator of class ''Event'' will get matched for these
two 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 my
compiler generates errors:
"ambiguous 3-way choice of conversion from ''struct Event'' in Boolean
context
All I''m trying to do is wrap an OS event in a class that can be treated like
a Boolean; can anyone explain what the ambiguity is and how it can be
resolved (preferable without casts)?
Best regards
Tim
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;
};解决方案
Tim Clacy wrote: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; }
ints *and* pointers are convertible to boolean (with the same
priority). So it is ambigious.
private:
Handle handle;
};
Shezan Baig wrote:Tim Clacy wrote:typedef struct tagHandle { }* Handle;
try instead:
struct Handle { SomeType* realHandle; };
bool eventIsSignalled(Handle hEvent);
struct Event
{
:
bool IsSignalled() const { return eventIsSignalled(*this); }
operator int () const { return IsSignalled(); }
operator Handle () const { return handle; }
ints *and* pointers are convertible to boolean (with the same
priority). So it is ambigious.
On Fri, 29 Apr 2005 18:45:08 +0200, "Tim Clacy"
<no*******@nospamphaseone.nospamdk> wrote:
Please illuminate; what operator of class ''Event'' will get matched for these
two cases [see Event outline later]:
[...]if (ev1 || ev2)
;
I would have thought ''operator int ()'' is the only obvious match, but my
compiler generates errors:
"ambiguous 3-way choice of conversion from ''struct Event'' in Boolean
context
All I''m trying to do is wrap an OS event in a class that can be treated like
a Boolean; can anyone explain what the ambiguity is and how it can be
resolved (preferable without casts)?
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不明确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!