I’m having some trouble converting a parameter:
I have this structure:
class XMLCO
{...};
class CO: public XMLCO
{...};
And my problem lies in this class in the constructor:
class ProcessUnit
{
public:
ProcessUnit( const CO& co );
private:
NetComm _ipComm;
};
The object _ipComm (of type NetComm) needs to be initialize with a XMLCO, but in this constructor I’m only given a CO which inherit XMLCO, so I though I could do some sort of a downcast like so in the constructor:
ProcessUnit::ProcessUnit( const CO& co )
{
CO temp = const_cast<CO>( co ); // to remove the const -- THIS LINE CAUSE THE PROBLEM (it gives me this error: the type in a const_cast must be a pointer or reference to an object type
CO* ptrTemp = &temp; // to make it a pointer
XMLCO* xmlcc = dynamic_cast<IOXMLDescCreationContext*>( ptrTemp );
_ipComm = new IONetworkComm( *xmlcc );
}
What I want to know is if there’s a simpler way of doing this (without changing anything to the generic structure) or if I’m doing something wrong.
Thanks
In your case you cannot cast down, only up. Luckily for you, this cast is implicit. Consequently, there’s nothing left for you to do. You can just assign:
Note, though, that this copies the object. Is this really what you want? Furthermore, removing
constis probably not only unnecessary, but wrong. Even if you corrected the syntax in your code, this would still not work. To diagnose this properly, you’d need to post the other definitions though.