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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:08:30+00:00 2026-06-10T23:08:30+00:00

I am trying to expose this C++ class using Boost.Python: class VAlgorithm { public:

  • 0

I am trying to expose this C++ class using Boost.Python:

class VAlgorithm {
public:

  VAlgorithm(const char *name);

  virtual ~VAlgorithm();

  virtual bool Initialize() = 0;
  virtual bool Process() = 0;
  virtual bool Finalize() = 0;

  virtual const char *GetName() const; // Default implementation in cpp file
}

My final goal is to define children of VAlgorithm in the python shell as python classes. Following this example, I defined a callback class:

class VAlgorithm_callback: public VAlgorithm {
public:
  VAlgorithm_callback(PyObject *p, const char *name) :
      self(p), VAlgorithm(name) {
  }
  const char * GetName() const {
    return call_method<const char *>(self, "GetName");
  }

  static const char * GetName_default(const VAlgorithm& self_) {
    return self_.VAlgorithm::GetName();
  }

private:
  PyObject *self;
};

Right now I am exposing only the class itself and the GetName() method. Since it is a virtual class, I placed this code inside BOOST_PYTHON_MODULE:

class_<VAlgorithm, VAlgorithm_callback, boost::noncopyable>("VAlgorithm", no_init) //
  .def("GetName", &VAlgorithm_callback::GetName_default); //

I can compile this and load the module in the python shell. Then I try to define a child class and call the GetName() default implementation defined in the C++ code:

>>> class ConcAlg1(VAlgorithm):
...   pass
... 
>>> c1 = ConcAlg1("c1")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: This class cannot be instantiated from Python
>>> class ConcAlg2(VAlgorithm):
...   def __init__(self, name):
...     pass
... 
>>> c2 = ConcAlg2("c2")
>>> c2.GetName()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
Boost.Python.ArgumentError: Python argument types in
    VAlgorithm.GetName(ConcAlg2)
did not match C++ signature:
    GetName(VAlgorithm)
>>> class ConcAlg3(VAlgorithm):
...   def __init__(self, name):
...     super(ConcAlg3, self).__init__(self, name)
... 
>>> c3 = ConcAlg3("c3")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in __init__
RuntimeError: This class cannot be instantiated from Python

I am not an expert (just facing these issues for the first time), but it seems to me that ConcAlg1 and ConcAlg3 try to instantiate a VAlgorithm object and fail because of the no_init parameter used when exposing VAlgorithm (I Can’t omit it or the code won’t compile), and ConcAlg2 can’t call GetName() because somehow it is not recognized as a child of VAlgorithm.
I must be doing something trivially wrong but I can’t figure out what (I am a novice of Boost.Python and extension). Can anyone please help me? Thanks

  • 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-10T23:08:31+00:00Added an answer on June 10, 2026 at 11:08 pm

    I have done things quite similar to this. Why don’t you follow what you already have found in your comment ?

    When you create an instance of a class deriving on VAlgorithm in Python, the VAlgorithm_callback will have to be instantiated in C++ to represent it. It’s not possible if you don’t declare any constructor in BOOST_PYTHON_MODULE.

    Doing the way its done in the example should be ok:

    class_<VAlgorithm, VAlgorithm_callback, boost::noncopyable>("VAlgorithm", init<std::string>())
    

    When you define the init of the child class, you can use following syntax to call the parent constructor (but your code may work too, that’s just the way I do). In the example you used, they don’t even define init so the parent one is called instead.

    class ConcAlg3(VAlgorithm):
        def __init__(self, name):
        VAlgorithm.__init__(self, name)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to expose a C++ library to python using boost-python. The library
I'm trying to expose the following c++ function to python using boost.python: template <typename
I'm trying to expose one of my classes to python using boost's python module.
I'm trying to expose my C++ Classes to Python using Boost.Python. Here is a
This question is kind of the next level of F# Set using custom class
I am trying to run a website using Python 2.7.1, Jinja 2.5.2, and CherryPy
I'm a newbie in boost.python and i'm getting this error that I would like
I want to take an object, let's say this object: public class BenchmarkList {
I'm trying to learn some C#.net. I'm just trying to expose the AdventureWorks database
I am trying to write our own RIA services provider to expose data from

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.