473,756 Members | 5,160 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_met hod("get2",&Der ived2::get2, "get2()\n") ;

in Derived::init_t ype.

inheritance.cxx :73: error: no matching function for call to `
Py::PythonExten sion<Base>::add _varargs_method (const char[5], Py::Object
(Derived2::*)(c onst Py::Tuple&), const char[8])'
/usr/local/include/python2.3/CXX/Extensions.hxx: 574: error: candidates are:
static void Py::PythonExten sion<T>::add_va rargs_method(co nst 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_mo dule*inheritanc e'
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::PythonExten sion<Derived1>, public Base

so that it gets it's own add_varargs_met hod 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::PythonExten sion<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().nam e("Base");
behaviors().doc ("My base class");
add_varargs_met hod("get", &Base::get, "get()\n");
}

void
Derived1::init_ type()
{
behaviors().nam e("Derived1") ;
behaviors().doc ("Derived 1");
}

void
Derived2::init_ type()
{
behaviors().nam e("Derived2") ;
behaviors().doc ("Derived 2");
add_varargs_met hod("get2", &Derived2::get2 , "get2()\n") ;
}

// the extension module
class inheritance_mod ule : public Py::ExtensionMo dule<inheritanc e_module>

{
public:
inheritance_mod ule()
: Py::ExtensionMo dule<inheritanc e_module>( "inheritanc e" )
{
Base::init_type ();
Derived1::init_ type();
Derived2::init_ type();

add_varargs_met hod("Derived1", &inheritance_mo dule::new_deriv ed1,
"Derived1(x )");
add_varargs_met hod("Derived2", &inheritance_mo dule::new_deriv ed2,
"Derived2(b )");
initialize( "The inheritance module" );
}

virtual ~inheritance_mo dule() {}

private:

Py::Object inheritance_mod ule::new_derive d1 (const Py::Tuple &args)
{
args.verify_len gth(1);
double val = Py::Float(args[0]);
return Py::Object( new Derived1(val) );
}
Py::Object inheritance_mod ule::new_derive d2 (const Py::Tuple &args)
{
args.verify_len gth(1);
if (!Base::check(a rgs[0]))
throw Py::TypeError(" Derived2(b) expecting a base instance");

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

extern "C"
DL_EXPORT(void)
initinheritance (void)
{
static inheritance_mod ule* inheritance = new inheritance_mod ule;

};


Jul 18 '05 #1
0 1462

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

Similar topics

2
4379
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 files containing some templates: adjacency_list.hpp and mem_fn.hpp can not compile. Does anyone have any solutions?
2
4338
by: Graham Banks | last post by:
Does using multiple inheritance introduce any more performance overhead than single inheritance?
4
2901
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 there shouldn't have been a "virtual" keyword for this purpose, but instead, a "nonvirtual" keyword! In teaching inheritance, you see the common example: class Vehicle {}
5
2182
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 multiple inheritance still be avoided? If yes, why multiple inheritance is introducted into C++?
10
2664
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 prefer composition over inheritance. Can anyone provide some reasons why this is the case (based on "real-world" experience)? For example, is structural inheritance more difficult to maintain? More difficult to test? Have a larger impact on...
14
12918
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 all obvious in VBA which lacks inheritance. I'm trying the explanation again now. I often find cases where a limited form of inheritance would eliminate duplication my code that seems impossible to eliminate otherwise. I'm getting very...
22
23383
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 examples?
45
6361
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 parameters, I get CS1501 (no method with X arguments). Here's a simplified example which mimics the circumstances: namespace InheritError { // Random base class. public class A { protected int i;
60
4932
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 'target' programming community herein) to get some community input and verify (or not) the following two statements. Few programmers (3 to7%) UNDERSTAND 'Strategic Functional Migration
6
3826
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 me somewhat as I have always thought you get code reuse "for free" with inheritance. Am I missing something?. Will someone care to explain ??
0
9431
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9255
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10014
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9844
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9819
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8688
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7226
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5289
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3780
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.