473,382 Members | 1,225 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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> {
public:
virtual Py::Object get(const Py::Tuple &args)=0;

static void init_type(void);

};

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

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

private:
double _val;
};
// binary operations on lazy values
class Derived2: public Base {
public:
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);
}
private:
Base* _b;
};
void
Base::init_type()
{
behaviors().name("Base");
behaviors().doc("My base class");
add_varargs_method("get", &Base::get, "get()\n");
}

void
Derived1::init_type()
{
behaviors().name("Derived1");
behaviors().doc("Derived 1");
}

void
Derived2::init_type()
{
behaviors().name("Derived2");
behaviors().doc("Derived 2");
add_varargs_method("get2", &Derived2::get2, "get2()\n");
}

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

{
public:
inheritance_module()
: Py::ExtensionModule<inheritance_module>( "inheritance" )
{
Base::init_type();
Derived1::init_type();
Derived2::init_type();

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

virtual ~inheritance_module() {}

private:

Py::Object inheritance_module::new_derived1 (const Py::Tuple &args)
{
args.verify_length(1);
double val = Py::Float(args[0]);
return Py::Object( new Derived1(val) );
}
Py::Object inheritance_module::new_derived2 (const Py::Tuple &args)
{
args.verify_length(1);
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"
DL_EXPORT(void)
initinheritance(void)
{
static inheritance_module* inheritance = new inheritance_module;

};


Jul 18 '05 #1
0 1435

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: AIM | last post by:
Error in msvc in building inheritance.obj to build hello.pyd Hello, I am trying to build the boost 1.31.0 sample extension hello.cpp. I can not compile the file inheritance.cpp because the two...
2
by: Graham Banks | last post by:
Does using multiple inheritance introduce any more performance overhead than single inheritance?
4
by: JKop | last post by:
I'm starting to think that whenever you derive one class from another, that you should use virtual inheritance *all* the time, unless you have an explicit reason not to. I'm even thinking that...
5
by: Morgan Cheng | last post by:
It seems no pattern defined by GoF takes advantage of multiple inheritance. I am wondering if there is a situation where multiple inheritance is a necessary solution. When coding in C++, should...
10
by: davidrubin | last post by:
Structural inheritance (inheriting implementation) is equivalent to composition in that a particular method must either call 'Base::foo' or invoke 'base.foo'. Apparantly, The Literature tells us to...
14
by: Steve Jorgensen | last post by:
Recently, I tried and did a poor job explaining an idea I've had for handling a particular case of implementation inheritance that would be easy and obvious in a fully OOP language, but is not at...
22
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete...
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
60
by: Shawnk | last post by:
Some Sr. colleges and I have had an on going discussion relative to when and if C# will ever support 'true' multiple inheritance. Relevant to this, I wanted to query the C# community (the...
6
by: Bart Simpson | last post by:
I remember reading on parashift recently, that "Composition is for code reuse, inheritance is for flexibility" see (http://www.parashift.com/c++-faq-lite/smalltalk.html#faq-30.4) This confused...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.