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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:16:17+00:00 2026-05-15T18:16:17+00:00

So I’m playing about with Python, C++0x, and SWIG 2.0. I’ve got a header

  • 0

So I’m playing about with Python, C++0x, and SWIG 2.0. I’ve got a header that looks like this:

#include <string>
#include <iostream>
#include <memory>
using namespace std;

struct Base {
  virtual string name();
  int foo;
  shared_ptr<Base> mine;
  Base(int);  
  virtual ~Base() {}
  virtual void doit(shared_ptr<Base> b) {
    cout << name() << " doing it to " << b->name() << endl;
    mine = b;
  }
  virtual shared_ptr<Base> getit() {
    return mine;
  }
};

struct Derived : Base {
  virtual string name();
  int bar;
  Derived(int, int);
};

Meanwhile, the interface file looks like this:

%module(directors="1") foo
%feature("director");

%include <std_string.i>

%include <std_shared_ptr.i>
%shared_ptr(Base)
%shared_ptr(Derived)

%{
#define SWIG_FILE_WITH_INIT
#include "foo.hpp"
%}

%include "foo.hpp"

My Python session then goes like this:

>>> import foo
>>> b = foo.Base(42)
>>> d = foo.Derived(23,64)
>>> b.doit(d)
Base doing it to Derived 
>>> g = b.getit()
>>> g
<foo.Base; proxy of <Swig Object of type 'std::shared_ptr< Base > *' at 0x7f7bd1391930> >
>>> d
<foo.Derived; proxy of <Swig Object of type 'std::shared_ptr< Derived > *' at 0x7f7bd137ce10> >
>>> d == g
False
>>> d is g
False
>>> d.foo == g.foo
True
>>> d.bar
64
>>> g.bar
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Base' object has no attribute 'bar'

I can’t seem to figure out how to retrieve the “original” proxy object here. Must I produce a function for each and every base class to perform the dynamic_pointer_cast? And if so, what of Director subclasses implemented in Python?

I get the feeling there’s a switch or feature I can turn on here to get SWIG to do the necessary table lookups and produce the object I want, but I haven’t found it yet.

(Note: The behavior is similar if I use raw pointers instead of shared pointers, and I can’t figure out how to get SWIG to dynamic_cast those either)

Update

If this sort of behavior (specifically, retrieving the most-derived proxy from a C++ container class that holds pointers to the base class) isn’t possible in SWIG, then how about SIP or some other wrapper generator for Python?

Update #2

Since SIP4 looks like it works a bit better as far as retrieving the wrapped object sensibly, I’ll change the question once again. Check my self answer below for details on my current issues. I’ll still accept a good answer for the original SWIG question since I prefer it overall, but my new questions are, basically:

  • How can I deal sanely with wrappers around shared_ptrs rather than raw pointers? If it helps, all of my classes subclass enable_shared_from_this from their generic base classes and expose an appropriate function to get the shared pointer.

  • How can I, using either of SIP4’s build systems (Makefile generator or distutils extension), build my little example project without having to first generate and install a shared library or manually edit the generated Makefile?

  • 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-15T18:16:18+00:00Added an answer on May 15, 2026 at 6:16 pm

    To (partially) answer my own question, SIP appears to do the right thing, both for the C++ “Derived” class as well as for a Python-level subclass — at least, when I use raw pointers.

    Looks like I’ll need to figure out how to get it to work with shared_ptrs (looks not quite as easy as %include <std_shared_ptr.i> in SWIG).

    Also, both of SIPs “build system” options (Makefile generator and distutils extension) seem a little odd. Neither example in their manual “just works” – it looks like they expect it to be obvious that you’re supposed to compile and install a shared library and header file in your library/include paths for the little Hello World library you’re trying to wrap. Perhaps there’s an “I just want to build and run this self-contained thing right here” option that I missed, but even python setup.py build_ext --inplace fails because it can’t find the wrapped header that I’ve placed in the current directory, which is obviously a confusing place for it, I know.

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Your page is rendering in Quirks Mode on IE8. When… May 15, 2026 at 7:22 pm
  • Editorial Team
    Editorial Team added an answer To me, the major advantage of EventListenerList is if the… May 15, 2026 at 7:22 pm
  • Editorial Team
    Editorial Team added an answer You should only implement IDisposable if your class holds instances… May 15, 2026 at 7:22 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.