|
Boost : |
From: rwgk_at_[hidden]
Date: 2001-07-16 13:08:08
--- In boost_at_y..., Peter.Bienstman_at_r... wrote:
> However, this does not seem to work, I have to explicitly define
> operator() in Material as well, in order to get it to work.
>
> Am I missing something here? It would be really useful if one did
not
> have to manually reregister functions in derived classes.
>
> Peter
operator()/__call__ seems to be the exception to the rule
that the methods are inherited when specifying declare_base().
See below.
Ralf
import declare_base
b = declare_base.base("1")
print b.get_major()
d = declare_base.derived("a", "b")
print d.get_major()
print d.get_minor()
print b()
print d.explicit_call()
print d()
% python tst_declare_base.py
1
a
b
1 through operator()
a through operator()
Traceback (most recent call last):
File "tst_declare_base.py", line 10, in ?
print d()
TypeError: object is not callable: <derived object at 140074870>
#include <boost/python/class_builder.hpp>
#include <string>
namespace {
class base
{
public:
base(const std::string& major) : m_major(major) {}
const std::string& get_major() const { return m_major; }
std::string operator()() const { return m_major + " through
operator()"; }
private:
std::string m_major;
};
class derived : public base
{
public:
derived(const std::string& major, const std::string& minor)
: base(major), m_minor(minor) {}
const std::string& get_minor() const { return m_minor; }
private:
std::string m_minor;
};
}
BOOST_PYTHON_MODULE_INIT(declare_base)
{
try
{
boost::python::module_builder this_module("declare_base");
boost::python::class_builder<base> py_base(this_module, "base");
boost::python::class_builder<derived> py_derived
(this_module, "derived");
py_derived.declare_base(py_base, boost::python::without_downcast);
py_base.def(boost::python::constructor<const std::string&>());
py_base.def(&base::get_major, "get_major");
py_base.def(&base::operator(), "__call__");
py_base.def(&base::operator(), "explicit_call");
py_derived.def(boost::python::constructor<const std::string&,
const std::string&>());
py_derived.def(&derived::get_minor, "get_minor");
}
catch(...)
{
boost::python::handle_exception();
}
}
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk