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 7948441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:43:24+00:00 2026-06-04T01:43:24+00:00

I have a method (or function) which returns a reference to a list of

  • 0

I have a method (or function) which returns a reference to a list of polymorphic objects:

class A {

};
class B : public A {

};


std::list<boost::shared_ptr<A> >& getList();

How do I expose such a function in boost::python so that when iterating on the list in python, I would see the different types of As and Bs ?

  • 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-04T01:43:25+00:00Added an answer on June 4, 2026 at 1:43 am

    First, make sure your classes are indeed polymorphic (i.e. they have at least one virtual function or a virtual destructor). Your example above doesn’t, though I’m sure your real use case does. Without that none of Boost.Python’s RTTI-based machinery for polymorphism will work.

    Then, if you’ve exposed both classes with Boost.Python and registered shared_ptr converters for them:

    #include <boost/python.hpp>
    
    namespace bp = boost::python;
    
    BOOST_PYTHON_MODULE(example) {
        bp::class_<A >("A");
        bp::register_ptr_to_python< boost::shared_ptr<A> >();
        bp::class_< B, bp::bases<A> >("B");
        bp::register_ptr_to_python< boost::shared_ptr<B> >();
    }
    

    …that’s all you need to do to make sure Python only ever sees the most-derived type. There’s no need to do anything special to ensure A is casted to B when possible.

    That still leaves the question of how to wrap a function that returns a container. The simplest is probably to use the indexing suite included with Boost.Python:

    http://www.boost.org/doc/libs/1_49_0/libs/python/doc/v2/indexing.html

    There are other options floating around the web (including a “version 2” of the indexing suite that is better in many respects, but isn’t included with Boost.Python), but for simple problems this is probably the most convenient.

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

Sidebar

Related Questions

So I have a class foo that has a method which returns an array
I have a method in my controller public function download($filepath){ $download_path = $_SERVER['DOCUMENT_ROOT']. mediabox/import;
Suppose you have a private static method called Inst() which allows the class to
I have the following javascript method: function 123_test_function(){ } The function is generated by
I have a method that is a pure function and takes a while to
I have one method that opens a file and passes off to another function
I want to have a java app running, using a function/method (with as little
Eg. I have following delegate method I want to use as a callback function
I have a function that use setInterval() method to watch css propertie change('cause in
I have a function that uses the SharePoint api so the method SPUtility.GetLocalizedString is

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.