Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7404889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:19:15+00:00 2026-05-29T05:19:15+00:00

I have two virtual classes I would like to wrap in boost python, I

  • 0

I have two virtual classes I would like to wrap in boost python, I want to be able to write Python class that extends them. The thing is, one of the classes has a method that return a reference of the other class, and I can’t figure how to do.

Here is a simplified version of code of class to be wrapped.

class Foo
{
    public:
    virtual ~Foo() {}
    virtual int a() = 0;
};

class Bar
{
    public:
    virtual ~Bar() {}
    virtual Foo const& b() = 0;
};

So I started wrapping about this way.

class FooWrap : public Foo, public wrapper<Foo>
{
    public:
    int a()
    {
        return this->get_override("a")();
    }
};

class BarWrap : public Bar, public wrapper<Bar>
{
    public:
    Foo const& b()
    {
        return this->get_override("b")();
    }
};

BOOST_PYTHON_MODULE(foobar)
{
    class_<FooWrap, boost::noncopyable>("Foo")
        .def("a", pure_virtual(&Foo::a))
        ;
    class_<BarWrap, boost::noncopyable>("Bar")
        .def("b", pure_virtual(&Bar::b))
        ;
}

And I get a compile error about “cannot instantiate abstract class […]
pure virtual function was not defined” “see declaration of ‘foo::a'”

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T05:19:16+00:00Added an answer on May 29, 2026 at 5:19 am

    I’ve been able to compile and run your code after I added call policy for Bar::b function:

    BOOST_PYTHON_MODULE(foobar)
    {
        class_<FooWrap, boost::noncopyable>("Foo")
            .def("a", pure_virtual(&Foo::a));
    
        class_<BarWrap, boost::noncopyable>("Bar")
            .def("b", pure_virtual(&Bar::b),
                 return_internal_reference<>());
    }
    

    Basically, it’s just means that lifetime of returned reference from Bar::b should be dependent on lifetime of Bar instance. You can read about call policies in boost docs.

    What compiler and boost version are you using? I’ve got the following descriptive error with boost 1.46.0 and gcc 4.6.1:

    error: no match for call to ‘(const boost::python::detail::specify_a_return_value_policy_to_wrap_functions_returning<const Foo&>) (const Foo&)’
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question, here are two classes below: class Base{ public: virtual void
I have 3 classes, two inherit from 1: public class Employee { private virtual
I have two classes: public class Parent { public virtual long? ID { get;
I have two classes: public class DocumentViewModel { public virtual string DocumentNumber { get;
HELP! I just setup a virtual host for two sites that have a lot
I have two classes, derived from a common class. The common class has a
Assume I have the following two classes: public class User : Entity { public
I have a number of classes that derive from a pure virtal base: class
I am using DBContext and have two classes whose properties are all virtual. I
Suppose I have two classes a base class and an inherited class as follows:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.