Extending classes written in C++ using SWIG

Lars Moastuen tjordah at start.no
Thu Nov 25 15:14:54 CET 2004

Im trying to extend a class written in C++ in Python and use this
extended class in a C++ call... I made an example to clarify:

-- Foo.h --
#ifndef FOO_H
#define FOO_H

#include <iostream>
using namespace std;

class Foo;
class Bar;

class Bar
	Bar() {};
	~Bar() {};
	virtual char* DoBar() const { return "Bar"; };

class Foo
	Foo() {};
	~Foo() {};
	virtual void DoFoo(Bar* someBar) { cout << someBar->DoBar() << endl;


-- Foo.cpp --
#include "Foo.h"

-- swig.i --
%module test
#include "Foo.h"

%include "Foo.h"

-- UseFoo.py --
#!/usr/bin/env python

from test import *;

class ExtendedBar(Bar):
	def __init__(self):
	def DoBar(self):
		return "ExtendedBar";

bar = ExtendedBar();
foo = Foo();


I now expect to get "ExtendedBar" as output from UseFoo.py (since I've
declared DoBar() as virtual, but I get "Bar".... I'm using SWIG 1.3.1
to create the bindings from Python to C++. (swig -python -c++)

Can anyone tell me why? Is there a way to remedy this??

Lars Moastuen

More information about the Python-list mailing list