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