here is the code for my question:
class ICommon
{
public:
virtual ICommon& operator=(const ICommon & p)const=0;
};
class CSpecial : public ICommon
{
public:
CSpecial& operator=(const CSpecial & cs)const
{
//custom operations
return *this;
}
};
CSpecial obj;
Basically: I want the interface ICommon to force it’s descendants to implement = operator but don’t want to have any typecasts in the implementation. The compiler says “can’t instantiate an abstract class.
Any help/advice will be appreciated.
To echo what Naveen said, the
operator=()defined in CSpecial isn’t compatible with the one defined in ICommon, and results in an overload rather than an override. While you can have covariant return types (as you’ve done), the arguments themselves can’t be covariant.Furthermore, you’ve defined the
ICommon::operator=()as const, which seems counterintuitive. In the derived class, you’ve made it non-const (as expected), but again, this makes the function signatures further incompatible.Naveen’s
clone()idea is probably your best bet. Otherwise, you can pass an ICommon const reference to your CSpecialoperator=()and attempt somedynamic_cast<>()magic internally, but that smells funny.Good luck!