interface and class cast exception

bwells

Regular
Joined
Feb 25, 2003
Messages
84
I have written an interface and a class that implements the interface. If I instantiate the class that implements the interface and then try and cast the class to be the type of the interface, I get a class cast exception.

I am using __interface which I though is what the documentation suggested. Perhaps this is wrong? What is the difference between _interface, interface and __interface?

When I use Intellisense in the debgger from the constructor of a class that implements the interface, it shows the interface as a struct. Is this correct?

Here is my interface:


#pragma once

using namespace System;

namespace COSP_9054_Interfaces
{
public __gc __interface ICOSP_9054_Connection
{
bool loadBoard( );
String* lastError( );
void* getConnection( );
};
}

I stepped into the constructor and observed in the debugger that the class being constructed does indeed implement the interface. Yet if I do an explicit cast to the Interface type:

ICOSP_9054_Connection* obj = (ICOSP_9054_Connection*) derivedClass;

I get a class cast exception.

I tried doing:

dynamic_cast<ICOSP_9054_Connection*>(derivedClass) as well, but this just returns null.

How come I cannot cast a derived class to be the type of the interface it implements?


thanks
Bryan
 
Back
Top