[C++-sig] implicitly_convertible / virtual base class

David Abrahams david.abrahams at rcn.com
Mon Jun 3 19:08:15 CEST 2002


From: "Achim Domma" <achim.domma at syynx.de>


> > Do you mean that DrawableBase is abstract?
> > If so, use class_<DrawableBase,boost::noncopyable>
> > Abstract classes cannot be copied.
>
> Yes, that was what I meant. It does not work if you define a ctor for
such a
> class, but after removing the ctor and using noncopyable it works now.
> Thanks.
>
> But my problem was not solved. I still have the following situation:
> DrawableStrokeColor is derived from DrawableBase. Drawable has a ctor
which
> accepts 'const DrawableBase&'. As far as I understand this means, that
> DrawableBase is implicit convertible to Drawable.

As long as the constructor is not marked "explicit", then yes.

> Therefor I added the
> following statement:
>
> implicitly_convertible<DrawableStrokeColor,Drawable>();
>
> The function I want to call is declared as:
>
> Image::draw(const Drawable&)
>
> If I try to use this function from python like:
>
> img.draw(DrawableStrokeColor('red'))
>
> I get the following error:
>
> Traceback (most recent call last):
>   File "E:\cvsroot\boost\libs\python\ImageMagick\test\magick.py", line
12,
> in ?
>     img.draw(DrawableStrokeColor('red'))
> TypeError: bad argument type for built-in operation
>
> Is the problem that draw expects a 'Drawable&' and no 'Drawable'?
                                                     ^^
I don't understand the question. Do you mean "not"   ^^ here?

If it actually expects Drawable&, then that is your problem. If it expects
const Drawable& as you wrote above, that's not your problem. Implicit
conversions produce rvalues, and rvalues cannot bind to non-const
references.

-Dave







More information about the Cplusplus-sig mailing list