468,161 Members | 1,952 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,161 developers. It's quick & easy.

inheritance and cxx

I am using pycxx 5.2.2 to implement some extension code and have a
problem relating to inheritance. I have a pure virtual base class and
two concrete derived classes. In the code below, everthing works fine
in that the derived classes can use the base method get, but if I try
and add a method only available to Derived2, eg, get2, I get a
compiler error on the call to

add_varargs_method("get2",&Derived2::get2, "get2()\n");

in Derived::init_type.

inheritance.cxx:73: error: no matching function for call to `
Py::PythonExtension<Base>::add_varargs_method(cons t char[5], Py::Object
(Derived2::*)(const Py::Tuple&), const char[8])'
/usr/local/include/python2.3/CXX/Extensions.hxx:574: error: candidates are:
static void Py::PythonExtension<T>::add_varargs_method(const char*,
Py::Object (T::*)(const Py::Tuple&), const char*) [with T = Base]
inheritance.cxx: In function `void initinheritance()':
inheritance.cxx:124: warning: unused variable `inheritance_module*inheritance'
error: command 'gcc' failed with exit status 1

How should I structure the code so that I can have inherited base
methods but also allow derived classes to have their own methods?

If I try defining Derived2 as

class Derived1: public Py::PythonExtension<Derived1>, public Base

so that it gets it's own add_varargs_method I get into trouble because
both of the base classes share many functions so I get multiple
inheritance errors.

Complete example follows...
#include <iostream>
#include <cmath>
#include <utility>
#include "CXX/Extensions.hxx"

class Base : public Py::PythonExtension<Base> {
virtual Py::Object get(const Py::Tuple &args)=0;

static void init_type(void);


// a mutable float
class Derived1: public Base {
Derived1(double val) : _val(val) {}
static void init_type(void);

Py::Object get(const Py::Tuple & args) {
return Py::Float(_val );

double _val;
// binary operations on lazy values
class Derived2: public Base {
Derived2(Base* b) : _b(b) {}

static void init_type(void);
Py::Object get(const Py::Tuple & args) {
return _b->get(args);

Py::Object get2(const Py::Tuple & args) {
return _b->get(args);
Base* _b;
behaviors().doc("My base class");
add_varargs_method("get", &Base::get, "get()\n");

behaviors().doc("Derived 1");

behaviors().doc("Derived 2");
add_varargs_method("get2", &Derived2::get2, "get2()\n");

// the extension module
class inheritance_module : public Py::ExtensionModule<inheritance_module>

: Py::ExtensionModule<inheritance_module>( "inheritance" )

add_varargs_method("Derived1", &inheritance_module::new_derived1,
add_varargs_method("Derived2", &inheritance_module::new_derived2,
initialize( "The inheritance module" );

virtual ~inheritance_module() {}


Py::Object inheritance_module::new_derived1 (const Py::Tuple &args)
double val = Py::Float(args[0]);
return Py::Object( new Derived1(val) );
Py::Object inheritance_module::new_derived2 (const Py::Tuple &args)
if (!Base::check(args[0]))
throw Py::TypeError("Derived2(b) expecting a base instance");

Base* b = static_cast<Base*>(args[0].ptr());
return Py::Object( new Derived2(b) );

extern "C"
static inheritance_module* inheritance = new inheritance_module;


Jul 18 '05 #1
0 1233

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Graham Banks | last post: by
4 posts views Thread by JKop | last post: by
5 posts views Thread by Morgan Cheng | last post: by
10 posts views Thread by davidrubin | last post: by
14 posts views Thread by Steve Jorgensen | last post: by
22 posts views Thread by Matthew Louden | last post: by
45 posts views Thread by Ben Blank | last post: by
6 posts views Thread by Bart Simpson | last post: by
1 post views Thread by gcdp | last post: by
reply views Thread by kamranasdasdas | last post: by
reply views Thread by gcreed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.