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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:14:38+00:00 2026-05-31T13:14:38+00:00

So just for the sake if having ”fun” I decided to emulate C++ member

  • 0

So just for the sake if having ”fun” I decided to emulate C++ member functions in C using pointer functions. Here is a simple code:

obj.h:

#ifndef OBJ_H
#define OBJ_H

#include <stdlib.h>
#include <stdio.h>

struct Obj{
    struct pObjVar* pVar;

    void (*read)(struct Obj*);
    void (*set) (struct Obj*, int);
};

struct Obj* newObj();
void deleteObj(struct Obj** obj);

#endif

obj.c:

#include "obj.h"

void readValue(struct Obj* this_);
void setValue (struct Obj* this_, int mValue_);

struct pObjVar{
    int mValue;
};

struct Obj* newObj(){
    struct Obj* tmp  = (struct Obj*)     malloc(sizeof(struct Obj));
    tmp->pVar        = (struct pObjVar*) malloc(sizeof(struct pObjVar));

    tmp->pVar->mValue = 0;
    tmp->read = readValue;
    tmp->set  = setValue;

    return tmp;
}

void deleteObj(struct Obj **obj){
    free((*obj)->pVar); (*obj)->pVar = NULL;
    free((*obj)); *obj = NULL;
}

void readValue(struct Obj *this_){
    printf("Value = %d\n",this_->pVar->mValue);
}

void setValue(struct Obj *this_, int mValue_){
    this_->pVar->mValue = mValue_;
}

main.c:

#include "obj.h"

int main(void)
{
    struct Obj* a = newObj();
    a->set(a, 10);
    a->read(a);
    deleteObj(&a);

    return 0;
}

Output:

>./a.out
 Value = 10    

In doing this, however, I figured I had to emulate the role of implicit this pointer by explicitly passing it to my member functions. This works fine, I guess, except that it makes the whole thing look weird!

If I wanted to pass the object, why would implement the functions as member functions? The only answer I found to it was maybe in cases where you would want to have a unified interface but various implementations? (something similar to C++ virtual functions?)

What are (if any) some other reasons to emulate member functions? Also, is there any way to get around passing the explicit this_ pointer at all?

EDIT: There was problem in the original code when passing the object. I was using &a by mistake for the read/set functions. You would only need it for the deleteObj if you want to set the pointer to NULL internally.

  • 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-31T13:14:38+00:00Added an answer on May 31, 2026 at 1:14 pm

    Just another way of writing:

    #define member(FUNC, ...) FUNC(this_, ## __VA_ARGS__)
    int reader(struct Obj *_this) {
      member(read, a, b, c);
      member(getch);
      return 0;
    }
    

    This can be used for implementing interfaces, inheritance and many C++ features, which were implemented like this in C with Classes times. In Linux kernel, file operations are implemented like this. File structure stores pointers to functions, so that each file system can store it’s own system call handlers that operate on/with the data in the structure.

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

Sidebar

Related Questions

I think I can explain myself without code, so for brevity's sake here we
Just for the sake of experimentation, I've been trying to determine different ways to
This question is just for a sake of knowledge. I don't have any practical
Let's say I have css1.css and css2.css. Just for the sake of keeping files
I'm just started on playing around with the canvas HTML5-object. For the sake of
Just looking for the first step basic solution here that keeps the honest people
I'm having a play around with using mod_rewrite to provide a better looking URL
This is my first attempt at using Backbone.js, so I decided to make a
I'm having some inexplicable behaviour using jQuery 1.4.2, and I'm beginning to think that
I am using the following code in Titanium Appcelerator to connect with remote Host:

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.