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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:48:32+00:00 2026-05-13T13:48:32+00:00

So I may have a rather unique use case here, but I’m thinking it

  • 0

So I may have a rather unique use case here, but I’m thinking it should work- But it’s not working correctly.

Basically, I have a class that uses a static factory method ( create ) that returns a shared_ptr to the newly created
instance of the class. This class also has a virtual function that I’d like to override from python and call from C++.

Maybe my code can express the thought more clearly than my words:

#include <string>
#include <iostream>
#include <boost/python.hpp>
#include <boost/enable_shared_from_this.hpp>

using namespace boost::python;
using namespace boost;

//~ Base Class ClassA
class ClassA
    : public enable_shared_from_this<ClassA>
{
protected:
    ClassA(){}
public:

    static shared_ptr<ClassA> create(){ return shared_ptr<ClassA>( new ClassA() ); }

    virtual void quack(){ std::cout<<"quacks like a ClassA Base"<<std::endl; }
};

//~ Wrapper for ClassA
struct WrapClassA : public ClassA, wrapper<WrapClassA>
{

    static shared_ptr<WrapClassA> create(){ return shared_ptr<WrapClassA>( new WrapClassA() ); }

    void quack()
    {
        std::cout<<"quacking like a Wrapper..."<<std::endl;
        if (override f = this->get_override("quack"))
        {
            std::cout<<"... override found!"<<std::endl;
            f();
        }
        else
        {
            std::cout<<"... no override found!"<<std::endl;
            ClassA::quack();
        }
    }

    void default_quack(){ this->ClassA::quack(); }
};

//~ C++ Call Test
void quack( shared_ptr<ClassA> ptr )
{
    ptr->quack();
}

//~ Exposing
BOOST_PYTHON_MODULE(TestCase)
{
    def( "quack", &quack );

    class_<ClassA, shared_ptr<WrapClassA>, noncopyable>( "ClassA", no_init )
        .def( "__init__", make_constructor(&WrapClassA::create) )
        .def( "quack", &ClassA::quack, &WrapClassA::default_quack )
    ;
}

//~ Main
int main()
{

    PyImport_AppendInittab( "TestCase", &initTestCase );
 Py_Initialize();

    boost::python::object main_module((boost::python::handle<>(boost::python::borrowed(PyImport_AddModule("__main__")))));
    boost::python::object main_namespace = main_module.attr("__dict__");

    boost::python::object testcase_module( (boost::python::handle<>(PyImport_ImportModule("TestCase"))) );
    main_namespace["TestCase"] = testcase_module;

    FILE* test_file = fopen("test.py", "r");
    PyRun_SimpleFile(test_file, "test.py");
    fclose( test_file );


    std::cin.get();

 return 0;
}

And here’s the contents of test.py:

print "Testing.."

class Derived( TestCase.ClassA ):
    def __init__( self ):
 TestCase.ClassA.__init__( self )
    def quack( self ):
 print( "Quacks like a derived class!" )


Ainst = TestCase.ClassA()
TestCase.quack( Ainst ) #Should print 'Quacks like ClassA Base'

Dinst = Derived()
TestCase.quack( Dinst ) #Should print 'Quacks like a derived class!', but doesn't!

And the output:

Testing… quacking like a Wrapper…
… no override found! quacks like a
ClassA Base quacking like a Wrapper…
… no override found! quacks like a
ClassA Base

So both the base and the class derived in python are acting the same. It looks like it’s not finding the override for some reason. I’m not sure but this may have something to do with the create() function. Any ideas would be greatly appreciated!

EDIT:

Added pythonquack to the py script – This works as expect:

def pythonquack( Inst ):
    print Inst
    Inst.quack()

Calling it for Ainst and Dinst says ‘Quacks like a Base’, and ‘Quacks like a Derived’, as I would expect. So for some reason the overrides aren’t getting passed back to C++.

  • 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-13T13:48:33+00:00Added an answer on May 13, 2026 at 1:48 pm

    I ended up rethinking my design using intrusive_ptrs. There was a little more work to be done with the wrappers than using shared_ptr, but it worked out fairly well. Thanks to everyone for their time.

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

Sidebar

Ask A Question

Stats

  • Questions 377k
  • Answers 377k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer After a long, long time I finally worked out why… May 14, 2026 at 8:50 pm
  • Editorial Team
    Editorial Team added an answer If one service implements both contracts then you should give… May 14, 2026 at 8:50 pm
  • Editorial Team
    Editorial Team added an answer There's an article on CodeProject that you might find useful. May 14, 2026 at 8:50 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.