Boost logo

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
> 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.

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
1 through operator()
a through operator()
Traceback (most recent call last):
  File "", 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
      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()"; }
      std::string m_major;

  class derived : public base
      derived(const std::string& major, const std::string& minor)
        : base(major), m_minor(minor) {}
      const std::string& get_minor() const { return m_minor; }
      std::string m_minor;

    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");

Boost list run by bdawes at, gregod at, cpdaniel at, john at