[C++-sig] Re: value_holder_back_reference::holds()

David Abrahams dave at boost-consulting.com
Sun Nov 2 20:37:56 CET 2003


"Niall Douglas" <s_sourceforge at nedprod.com> writes:

> This goes as follows:
>
> template <class Value, class Held>
> void* value_holder_back_reference<Value,Held>::holds(
>     type_info dst_t)
> {
>     type_info src_t = python::type_id<Value>();
>     Value* x = &m_held;
>     
>     if (dst_t == src_t)
>         return x;
>     else if (dst_t == python::type_id<Held>())
>         return &m_held;
>     else
>         return find_static_type(x, src_t, dst_t);
> }
>
> Q: Why is find_static_type() used here? Shouldn't it use 
> find_dynamic_type() when the value is polymorphic?

No, we already know the most-derived type it can be: Held.  Unless
you've introduced multiple levels of inheritance between Value and
Held (you're not supposed to do that) there's no way you can
correctly downcast to anything else from Value.

> I only ask because I am passing "self" in python to a class 
> constructor which expects a FXComposite pointer. 

There's too much information missing and the little you've provied is
too vague.  How are you passing it?  What is a "class constructor?"
Metaclasses construct classes when called.  Do you actually mean an
__init__ function?

Instead of trying to describe it in English, please just show a small
code example.

> self is python class

Really, a class?  Not an instance of the class?

> MainWindow which inherits off C++ class FXMainWindow 

More accurately, I presume: you derived self.__class__ in Python from
the Python class wrapper for the C++ class FXMainWindow.

> which someway down the line inherits off FXComposite, so the above
> should work.

I don't understand what you mean by "the above should work".  When
constructing a C++ class instance in a Python object, there is no
check to see that the Python object already holds an instance of the
C++ class, since, after all, the C++ object has not been
constructed.  

> Unfortunately find_static_type() is not finding the FXComposite and 
> is returning zero. If however I alter the "polymorphic" parameter in 
> convert_type() (called by find_static_type()) to true, it *does* find 
> the FXComposite correctly. Of course FXComposite has virtual 
> functions, therefore a virtual function table for RTTI to work with.

That surprises me, but I guess anything is possible.

> If this is not known behaviour (it seems odd that find_static_type() 
> is not finding base classes), I'll get to work on a repeatable 
> example. Just say the word.

"the word"

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com





More information about the Cplusplus-sig mailing list