I'm having difficulty defining and using a conversion operator to a base class. Consider the following:
class base
{
public:
base(const base&);
base& operator=(const base&);
//some stuff
};
class child : public base
{
public:
operator base() const;
//some more stuff
};
int main()
{
child c;
base b=c;
b=c;
}
If I attempt to convert a child to a base, operator base()
is never called (even if I make the conversion explicit). Instead, the copy constructor or assignment operator from the base class is called directly, without a conversion.
How can I cause operator base()
to be called when a child
is assigned (or copy-constructed) to a base
?