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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:45:01+00:00 2026-05-27T01:45:01+00:00

I am looking for a simple way to expose a C++ class instance to

  • 0

I am looking for a simple way to expose a C++ class instance to a python embedded interpreter.

  • I have a C++ library. This library is wrapped (using swig for the moment) and I am able to use it from the python interpreter
  • I have a C++ main program which instanciates a Foo class from my library and embeds a python interpreter

I would like to expose my C++ world instance of Foo to the python world (and seen as a Foo class).

Is this possible, if so, how?

I think it’s almost like in the first answer of :
boost::python::ptr or PyInstance_New usage

I guess this means I should use boost.Python to wrap my library?

My only goal is to manipulate my C++ instance of Foo in the embedded python interpreter (not sure that it can be done with the previous method).

In fact, I already have exposed my Foo class to python (with swig).

What I have:

my Foo class:

class Foo{...};

my wrapped library (including the Foo class) exposed to python: so I can start the python interpreter and do something like this :

import my_module
foo=my_modulde.Foo()

What I want:

Having a C++ main program which embeds a python interpreter and manipulates C++ world variables.

int main(int argc, char **argv)
{
    Foo  foo;   // instanciates foo
    
    Py_Initialize();

    Py_Main(argc, argv); // starts the python interpreter
                         // and manipulates THE foo instance in it

    Py_Finalize();
    
    return 0;
}
  • 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-27T01:45:02+00:00Added an answer on May 27, 2026 at 1:45 am

    Boost python Allows you to expose c++ classes to python in a very tightly integrated way – you can even wrap them so that you can derive python classes from your c++ ones, and have virtual methods resolved to the python overrides.

    The boost python tutorial is a good place to start.


    edit:

    You can create a c++ object and pass a reference to it to an internal python interpreter like this:

    #include <boost/shared_ptr.hpp>
    #include <boost/make_shared.hpp>
    #include <boost/python.hpp>
    #include <string>
    #include <iostream>
    
    namespace bp = boost::python;
    
    struct Foo{
        Foo(){}
        Foo(std::string const& s) : m_string(s){}
        void doSomething() {
            std::cout << "Foo:" << m_string << std::endl;
        }
        std::string m_string;
    };
    
    typedef boost::shared_ptr<Foo> foo_ptr;
    
    BOOST_PYTHON_MODULE(hello)
    {
        bp::class_<Foo, foo_ptr>("Foo")
            .def("doSomething", &Foo::doSomething)
        ;
    };
    
    int main(int argc, char **argv)
    {
        Py_Initialize();
        try {
            PyRun_SimpleString(
                "a_foo = None\n"
                "\n"
                "def setup(a_foo_from_cxx):\n"
                "    print 'setup called with', a_foo_from_cxx\n"
                "    global a_foo\n"
                "    a_foo = a_foo_from_cxx\n"
                "\n"
                "def run():\n"
                "    a_foo.doSomething()\n"
                "\n"
                "print 'main module loaded'\n"
            );
    
            foo_ptr a_cxx_foo = boost::make_shared<Foo>("c++");
    
            inithello();
            bp::object main = bp::object(bp::handle<>(bp::borrowed(
                PyImport_AddModule("__main__")
            )));
    
            // pass the reference to a_cxx_foo into python:
            bp::object setup_func = main.attr("setup");
            setup_func(a_cxx_foo);
    
            // now run the python 'main' function
            bp::object run_func = main.attr("run");
            run_func();
        }
        catch (bp::error_already_set) {
            PyErr_Print();
        }
    
        Py_Finalize();
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for a simple way to have this done. I would to have
I have been looking for a simple way to allow a similar functionality to
I'm looking for a simple way to feed autocomplete with a dictionary and have
I'm looking for a simple way to draw 3D text on QGLWidget without using
I'm looking for a simple way to encrypt my soap communication in my C#
I am looking for a simple way to get a mime type where the
I'm looking for a simple way to let users define a set of rules
I'm looking for a simple way to translate long to String and back in
I am looking for a simple way (UDF?) to establish the similarity between strings.
I'm looking for as simple way to create an identity set. I just want

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.