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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:45:38+00:00 2026-06-07T02:45:38+00:00

I am building a Swig interface to a templated class. In my pyinterface.i file,

  • 0

I am building a Swig interface to a templated class. In my pyinterface.i file, I declare

%template (myclass) MyClass<int>;

Now what I want to do, is add a field variable to my new class, which I assume should be done as

%extend MyClass<int>{
    double x;
}

However, this complains that there is no

myclass_get_x

method defined. So if I try to define this by modifying the above to:

%extend MyClass<int>{
    double x;
    double MyClass<int>_get_x(MyClass<int> *f){
            return (*f)->x;
    }
}

Then I get syntax errors.

I tried doing this as:

%extend myclass{
    double x;
    double myclass_get_x(myclass *f){
            return (*f)->x;
    }
}

But this also throws errors, as myclass doesn’t seem to be understood yet.

  • 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-07T02:45:39+00:00Added an answer on June 7, 2026 at 2:45 am

    When you extend a class with %extend you can’t really add any new member variables, only member functions. It will assume the existence of a get/set function if you do this in %extend.

    The reason for this is that the storage (i.e. memory) for the new member variables would have to live somewhere, but there’s no obvious safe place to put it. You can’t modify the original C++ class, because if you do you’ll end up with undefined behaviour when some other, pre-compiled code uses the old definition of the class; there’s no way to retrospectively apply the new definition. You can’t put it in the proxy that gets generated in the target language, because there isn’t a 1:1 mapping between instances of C++ classes and proxies in the target language. (Consider two functions that both return the same global instance via a pointer for the trivial example of how this may occur).

    If you wanted to implement the get/set functions to do something useful (e.g. here I used a global map to store the extra data) you don’t want to implement them inside %extend For example, given test.hh with just:

    template <typename T>
    struct Foo {};
    

    You can use a std::map to do what you’re trying to do by writing your own gets and sets as free functions in the wrapper – %{ %} just passes the code straight through:

    %module test
    
    %include "test.hh"
    
    %{
    #include "test.hh"
    %}
    
    %{
    #include <map>
    static std::map<Foo<int>*, double> extra_stuff;
    
    const double& Foo_Sl_int_Sg__x_get(Foo<int>* f) {
      return extra_stuff[f];
    }
    
    void Foo_Sl_int_Sg__x_set(Foo<int>* f, const double& d) {
      extra_stuff[f] = d;
    }
    %}
    
    %template(FooInt) Foo<int>;
    
    %extend Foo<int> {
      double x;
    }
    

    The get/set functions are mangled by SWIG’s own mangling system because it’s a template. I just looked in the generated wrapper class to see what they were called. (I think there might be a smarter way to do that but I couldn’t figure it out from the documentation yet). If it wasn’t a template the name of the functions would be much simpler.

    Note that there’s no provision here for ever removing entries from this map – it will just grow indefinitely. (You can work around that for example by providing a typemap that calls an extra function at the end of an Object’s life). You also need to be careful about threads.

    Since you didn’t specify what language you were using I tested the above code with Java:

    public class run {
      public static void main(String[] argv) {
        System.loadLibrary("test");
        FooInt f = new FooInt();
        f.setX(0.1);
        System.out.println(f.getX());
      }
    }
    

    Alternatively if you really want to you can write code to ensure that there is a unique single proxy instance in the the target language, by writing a typemap that checks if an proxy has been created instead of always creating one, but that raises issues with reference counting and thread safety that are hard to address efficiently and generically, which is why it’s not the default behaviour.

    The simplest workaround in a lot of cases (e.g. iterators in the target language) though is to use %inline to declare and define and wrap all at once an entirely new extra class that that provides the functionality you want.

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

Sidebar

Related Questions

Building an NSCharacter set to restrict a UITextField for entering user names. I want
Building a simple app here; the methods in question: static coin class public static
Building a comment system with Ajax and JQuery and I want the div the
Building my baseclasses for user interface controls is getting there. I have command buttons
Building on from a previous query regarding Class::Struct vs Object::Accessor, I'd like to find
Building on this question , now I have another problem. Given this, shipments =
Building an inventory system. I have lots of products and each product has three
Building a new Mobile Web Platform for Mobile Users to purchase & download content
building a site using PHP and MySQL that needs to store a lot of
Building a Django app on a VPS. I am not very experienced with setting

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.