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

  • SEARCH
  • Home
  • 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 7918511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T15:32:14+00:00 2026-06-03T15:32:14+00:00

I have python embedded in a C++ application. The C++ calls python and passes

  • 0

I have python embedded in a C++ application. The C++ calls python and passes it as an argument a C++ object. that object has some virtual functions and can be a base class for some derived class. How do I make boost::python understand that it’s a virtual function?

consider the following:
in C++:

class Base {
public:
  virtual void func();
}

class Derived {
public:
  virtual void func();
}

BOOST_PYTHON_MODULE(module_api) {
  class_<Base>("Base")
    .def("func", &Base::func);  // ?? what should I put here?
}

int main() {
  //... initialization
  Derived derived;
  main_namespace["pyentry"](&derived);
}

in python:

def pyentry(baseref):
  baseref.func()    # here I want Derived::func() to be called

What am I doing wrong here?

  • 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-06-03T15:32:15+00:00Added an answer on June 3, 2026 at 3:32 pm

    The problem here is that Boost.Python is deep-copying your derived-class object and slicing it into a base object when it converts it to Python; there’s no need to tell Boost.Python about a function being virtual at all unless you need to override it in Python (and it sounds like you don’t).

    It’s doing that copy to be safe: it’s making certain that the object Python is given won’t be deleted by C++ while Python still has a reference to it. And it’s slicing it to Base – I think – because it doesn’t know anything about Derived.

    I can think of two ways to fix this:

    • Provide a trivial wrapper for Derived. Boost.Python will still copy it when converting it to Python, but it won’t slice it into a Base anymore when it does so.

    • Register a shared_ptr conversion for Base (via register_ptr_to_python< boost::shared_ptr<Base> >()), create your Derived instance in a shared_ptr, and pass that as the argument to your Python function. Now Boost.Python knows the C++ object can’t get deleted while the Python object exists, because the Python wrapper holds a shared_ptr reference to it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working in an embedded Linux environment and I have some Python code which
I am looking for a small database that can be embedded into my Python
I have an embedded scripting engine in my C# application that uses IronPython 2.
I have a python program in an embedded system that needs to write to
Assuming I have some embedded python code containing a function foo, what is the
I have Python nested list that I'm trying to organize and eventually count number
i have an issue i could use some help with, i have python list
I have an embedded python interpreter in my program. I'd like to export a
I'm creating a GUI application that can monitor and manipulate a stream of messages.
I have installed Python 2.6.7 in $HOME/local of a machine which already has a

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.