I'm having trouble porting some code to WIN32:

SomeClass::~SomeClass()
{
if (IsOpen())
{
if (typeid( *this) != typeid( SomeClass))
throw new SomeException(this, "Cannot close from a base class") ;
Close() ;
}
...

On the typeid comparison, I'm getting:

error C2678: binary '==' : no operator found which takes a left-hand operand
of type 'const type_info' (or there is no acceptable conversion)

How do I fix this?

Thanks.

Re: typeid comparison problem by Igor

Igor
Mon Aug 22 17:15:47 CDT 2005

uncaged <uncaged@discussions.microsoft.com> wrote:
> I'm having trouble porting some code to WIN32:
>
> SomeClass::~SomeClass()
> {
> if (IsOpen())
> {
> if (typeid( *this) != typeid( SomeClass))
> throw new SomeException(this, "Cannot close from a base
> class") ; Close() ;
> }
> ...
>
> On the typeid comparison, I'm getting:
>
> error C2678: binary '==' : no operator found which takes a left-hand
> operand of type 'const type_info' (or there is no acceptable
> conversion)

Have you included typeinfo.h?
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925



Re: typeid comparison problem by Doug

Doug
Mon Aug 22 17:25:57 CDT 2005

On Mon, 22 Aug 2005 15:10:01 -0700, "uncaged"
<uncaged@discussions.microsoft.com> wrote:

>I'm having trouble porting some code to WIN32:
>
>SomeClass::~SomeClass()
>{
> if (IsOpen())
> {
> if (typeid( *this) != typeid( SomeClass))
> throw new SomeException(this, "Cannot close from a base class") ;
> Close() ;
> }
>...
>
>On the typeid comparison, I'm getting:
>
>error C2678: binary '==' : no operator found which takes a left-hand operand
>of type 'const type_info' (or there is no acceptable conversion)
>
>How do I fix this?

You need to #include <typeinfo>. VC++ essentially forward declares
std::type_info everywhere, but you need to #include the header to make use
of it. That said, inside a dtor, the dynamic type of an object is the same
as its static type, so I think your comparison is always false. Even if the
test rightly goes into Close(), I'd have to question why you're doing it at
all. If Close is public, it seems like the restriction captured in the
exception error message violates the substititutability principle.

--
Doug Harrison
VC++ MVP

Re: typeid comparison problem by uncaged

uncaged
Mon Aug 22 17:29:03 CDT 2005

Thanks, Igor. That was exactly my problem!


"Igor Tandetnik" wrote:

> uncaged <uncaged@discussions.microsoft.com> wrote:
> > I'm having trouble porting some code to WIN32:
> >
> > SomeClass::~SomeClass()
> > {
> > if (IsOpen())
> > {
> > if (typeid( *this) != typeid( SomeClass))
> > throw new SomeException(this, "Cannot close from a base
> > class") ; Close() ;
> > }
> > ...
> >
> > On the typeid comparison, I'm getting:
> >
> > error C2678: binary '==' : no operator found which takes a left-hand
> > operand of type 'const type_info' (or there is no acceptable
> > conversion)
>
> Have you included typeinfo.h?
> --
> With best wishes,
> Igor Tandetnik
>
> With sufficient thrust, pigs fly just fine. However, this is not
> necessarily a good idea. It is hard to be sure where they are going to
> land, and it could be dangerous sitting under them as they fly
> overhead. -- RFC 1925
>
>
>