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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T22:09:10+00:00 2026-06-05T22:09:10+00:00

I’ve got a simple C class I have implemented, using function pointers in a

  • 0

I’ve got a simple C “class” I have implemented, using function pointers in a struct to implement the member functions, and passing a pointer to the struct as the first argument to each function, similar to the implicit “this” pointer in C++.

%module mytest
%{
typedef struct mytest mytest;

struct mytest {
  int data;
  int (*func1)(mytest *,int);
  void (*func2)(mytest *,int);
};

int f1(mytest *me,int n) { return me->data + n; }
void f2(mytest *me,int n) { me->data += n; }

mytest *mytestNew(int n) {
  mytest *me = (mytest*) malloc(sizeof(mytest));
  me->data = n;
  me->func1 = f1;
  me->func2 = f2;
  return me;
}

%}

typedef struct mytest mytest;

struct mytest {
  int data;
  int func1(mytest *,int);
  void func2(mytest *,int);
};

extern mytest *mytestNew(int n);

Now my problem is, when the interface is created to whatever language I choose in the front end, I wind up having to explicitly pass the “this” pointer to the object, even though the language itself supports hiding this.

For instance, suppose I choose Python. I have to do something like this:

from mytest import *
m = mytestNew(1)
m.func1(m,0)

Where what I really want is to do it like this:

from mytest import *
m = mytestNew(1)
m.func1(0)

I know I could just write some wrapping code, but for my actual project I have a lot of functions in a lot of objects of existing C code, and multiplying this by every language that I want to support, this is just too much work! Is there some way to get SWIG to do this automatically?

  • 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-05T22:09:12+00:00Added an answer on June 5, 2026 at 10:09 pm

    You can do this in a language neutral way in SWIG with just two typemaps provided you name the parameter something consistent in the SWIG interface as well as the definitions to allow the typemaps to be applied selectively. (Unless you wanted all pointers to mytest to become “this” pointers by default of course)

    The typemaps you need are:

    // Make sure the wraqpped function doesn't expect an input for this:
    %typemap(in,numinputs=0) mytest *me "$1=NULL;"
    // Slightly abuse check typemap, but it needs to happen after the rest of the arguments have been set:
    %typemap(check) mytest *me {
      $1 = arg1;
    }
    

    The check typemap isn’t really intended for use like this, but it’s the easiest way to get the code to be injected after the arguments have been extracted from the target language and before the actual call is made.

    You can also simplify the module with the help of a macro to avoid having to write and keep in sync the mapping between the function pointers and the members trick. I ended up with test.h as:

    #ifdef SWIG
    #define MEMBER(name, args) name args
    #else
    #define MEMBER(name, args) (*name) args
    #endif
    
    typedef struct mytest mytest;
    
    struct mytest {
      int data;
      int  MEMBER(func1,(mytest *me,int));
      void MEMBER(func2,(mytest *me,int));
    };
    

    And the corresponding interface file (test.i):

    %module test
    
    %{
    #include "test.h"
    
    static int f1(mytest *me,int n) { return me->data + n; }
    static void f2(mytest *me,int n) { me->data += n; }
    %}
    
    %extend mytest {
      mytest(int n) {
        $self->data = n;
        $self->func1 = f1;
        $self->func2 = f2;
      }
    }
    
    %typemap(in,numinputs=0) mytest *me "$1=NULL;"
    %typemap(check) mytest *me {
      $1 = arg1;
    }
    
    %include "test.h"
    

    (This interface file provides a constructor that “creates” the “object” exactly how a Java programmer would expect – you can call new and it sets the function pointers behind the scenes)

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
I am doing a simple coin flipping experiment for class that involves flipping a
I have thousands of HTML files to process using Groovy/Java and I need to
I'm making a simple page using Google Maps API 3. My first. One marker
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a jquery bug and I've been looking for hours now, I can't
I've got a string that has curly quotes in it. I'd like to replace

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.