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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T17:59:25+00:00 2026-06-10T17:59:25+00:00

Possible Duplicate: pass callback from python to c++ using boost::python I have to make

  • 0

Possible Duplicate:
pass callback from python to c++ using boost::python

I have to make a python function as callback of a c++ function.How to do?Where can I find some examples??
I want to use boost.python.

  • 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-10T17:59:27+00:00Added an answer on June 10, 2026 at 5:59 pm

    My approach to this (not the only way)

    In C++ I have a class that provides a callback in the following way:

    struct Mesh {
     template<class Visitor>
      void visitChildren(Visitor& v)
      {
         [...]
         v.visit([...])
      }
    }
    

    and then when I export the class to python (still C++)

    struct ChildrenVisitor 
    {
       ChildrenVisitor(PyObject* self)
            : self(self) 
       {}
    
      void visit( /* .... */ )
      {
        call_method<void>(self, "visit" /*, [...] */ );
      }
    
     private:
        PyObject* self; // 1
    };
    

    The export of the visitor itself to python

    typedef ChildrenVisitor ClassT;
    class_<ClassT, boost::noncopyable >("ChildrenVisitor",
        init<PyObject*>() );
    

    And for the export of Mesh you do

    .def("visitChildren", &Mesh::template visitChildren<ChildrenVisitor> )
    

    I always used ... where you can insert any of your arguments.

    In python you do something like this

    class MyVisitor(ChildrenVisitor): 
      def __init__(self):
        ChildrenVisitor.__init__(self,self)
    
      def visit(self):
        # do whatever you want
    

    I also like to make a subclass of ChildrenVisitor that accepts lambda functions, that makes writing visitors in python single lines.


    Oh, btw. if you want to call the function at some later point then you would need to change the C++ implementation to something like this

    struct ChildrenVisitorBase 
    {
      virtual void visit( /* .... */ ) = 0;
    };
    
    struct Mesh {
      void registerCallback(shared_ptr<ChildrenVisitorBase> v)
      {
         visitors.push_back(v)
      }
    
      void doSomeWork() {
        // ...
        for(int i=0; i < visitors.size(); ++i)
          visitors[i]->visit( /* ... */ )
      }
    
      std::vector<  shared_ptr<ChildrenVisitorBase> > visitors;
    }
    

    And make ChildrenVisitor implement ChildrenVisitorBase.

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

Sidebar

Related Questions

Possible Duplicate: How can I pass a parameter to a setTimeout() callback? Is there
Possible Duplicate: pass data from Action To Another Action I have a view in
Possible Duplicate: Pass correct “this” context to setTimeout callback? I have a problem with
Possible Duplicate: Is it possible to pass parameters by reference using call_user_func_array()? I have
Possible Duplicate: How to pass values to a callback function at the time of
Possible Duplicate: pass parameter in g:remoteLink as result of javascript function I am trying
Possible Duplicate: pass by reference not working I was going through some of the
Possible Duplicate: How to Pass String to another forms from one form in c#
Possible Duplicate: How to pass object from one activity to another in Android I
Possible Duplicate: How do you pass a function as a parameter in C? 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.