By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,714 Members | 1,363 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,714 IT Pros & Developers. It's quick & easy.

Wrapping classes with pure virtual functions

P: n/a
Hi,
I'm having problems wrapping a hierarchy of classes, actually having
problems wrapping the base class. I don't need to use the WrapClass
mechanism since I don't want to override classes in Python. My code
boils down to:

class Base
{
public:
virtual ~Base()
{}

virtual void f() = 0;
};

class Derived :
public Base
{
public:
virtual void f()
{}

void g()
{}
};

int main()
{
boost::python::class_<Baseclass_base("Base");
boost::python::class_<Derivedclass_derived("Derive d");
return 0;
}

The first line inside main (the one exporting Base) is causing the
compile time error:
error C2259: 'Base' : cannot instantiate abstract class...
The probem seems to be that struct value_holder<Baseholds a Base
member instance by value, and this is not possible since Base can't be
instantiated.
I'm using Visual Studio 2005 and boost 1.33.1.
Thanks

Dec 14 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Thu, Dec 14, 2006 at 01:57:10PM -0800, ga****************@gmail.com wrote:
Hi,
I'm having problems wrapping a hierarchy of classes, actually having
problems wrapping the base class. I don't need to use the WrapClass
mechanism since I don't want to override classes in Python. My code
boils down to:

class Base
{
public:
virtual ~Base()
{}

virtual void f() = 0;
};

class Derived :
public Base
{
public:
virtual void f()
{}

void g()
{}
};

int main()
{
boost::python::class_<Baseclass_base("Base");
Why are you trying to make this type visible to python? It is pure virtual,
you can never instantiate it.
boost::python::class_<Derivedclass_derived("Derive d");
return 0;
}

The first line inside main (the one exporting Base) is causing the
compile time error:
error C2259: 'Base' : cannot instantiate abstract class...
The probem seems to be that struct value_holder<Baseholds a Base
member instance by value, and this is not possible since Base can't be
instantiated.
I'm using Visual Studio 2005 and boost 1.33.1.
Thanks

--
http://mail.python.org/mailman/listinfo/python-list
Dec 17 '06 #2

P: n/a

Chris Lambacher wrote:
On Thu, Dec 14, 2006 at 01:57:10PM -0800, ga****************@gmail.com wrote:
Hi,
I'm having problems wrapping a hierarchy of classes, actually having
problems wrapping the base class. I don't need to use the WrapClass
mechanism since I don't want to override classes in Python. My code
boils down to:

class Base
{
public:
virtual ~Base()
{}

virtual void f() = 0;
};

class Derived :
public Base
{
public:
virtual void f()
{}

void g()
{}
};

int main()
{
boost::python::class_<Baseclass_base("Base");
Why are you trying to make this type visible to python? It is pure virtual,
you can never instantiate it.
Because I want to use subclasses of Base polymorphically from Python.
Python will receive an instance of some Base subclass through a another
exported function.
boost::python::class_<Derivedclass_derived("Derive d");
return 0;
}

The first line inside main (the one exporting Base) is causing the
compile time error:
error C2259: 'Base' : cannot instantiate abstract class...
The probem seems to be that struct value_holder<Baseholds a Base
member instance by value, and this is not possible since Base can't be
instantiated.
I'm using Visual Studio 2005 and boost 1.33.1.
Thanks

--
http://mail.python.org/mailman/listinfo/python-list
Dec 17 '06 #3

P: n/a
ga****************@gmail.com wrote:
Chris Lambacher wrote:
On Thu, Dec 14, 2006 at 01:57:10PM -0800, ga****************@gmail.com wrote:
Hi,
I'm having problems wrapping a hierarchy of classes, actually having
problems wrapping the base class. I don't need to use the WrapClass
mechanism since I don't want to override classes in Python. My code
boils down to:
>
class Base
{
public:
virtual ~Base()
{}
>
virtual void f() = 0;
};
>
class Derived :
public Base
{
public:
virtual void f()
{}
>
void g()
{}
};
>
int main()
{
boost::python::class_<Baseclass_base("Base");
Why are you trying to make this type visible to python? It is pure virtual,
you can never instantiate it.

Because I want to use subclasses of Base polymorphically from Python.
Python will receive an instance of some Base subclass through a another
exported function.
I don't know much about Boost. Does it have anything like
boost::python::pointer_<Base>?

You can't get polymorphism by direct access to a class. C++ compiler
always knows the exact type of a direct, non-pointer-accessed object.
So even if you could create a class_<Base>, it would only ever be a
Base, and never a derived class. You have to use pointers to get
polymorphism.

As an alternative, consider wrapping the derived classes instead. It
might not be much more work if Boost wraps everything nicely.
Carl Banks

Dec 17 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.