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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T08:12:42+00:00 2026-06-02T08:12:42+00:00

I want to do some object-oriented style programming in C using polymorphism, where my

  • 0

I want to do some object-oriented style programming in C using polymorphism, where my interface class contains a pointer to a table of functions. Example something like:

/* Implement polymorphism in C, Linux kernel-style */
struct statement {
    const struct statement_ops *ops;
    struct list_head list;      /* when on master input list */
    void *private;          /* pointer to type-specific data */
};

struct statement_ops {
    int (*analyse)(void *private, int pc);
    int (*get_binary_size)(void *private);
};

void user(void)
{
    struct statement *s = make_a_statement();
    if (s->ops->analyse(s->private, foo))
        blah blah;
}

I’d like to be able to write something without explicitly passing s->private into every “method”. Any ideas? Some macro tricks maybe?

  • 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-02T08:12:44+00:00Added an answer on June 2, 2026 at 8:12 am

    If this is part of the public interface, you can add accessor functions. A hidden benefit is that you can do sanity checks and other work in the accessor. (Note I called the “this” pointer “o”, as in “object”. I prefer it that way for consistency.)

    int statement_analyse (struct statement *o, int pc)
    {
        assert(pc >= 0);
    
        int ret = o->ops->analyse(o->private, pc);
        assert(ret >= 0);
    
        return ret;
    }
    

    You can now call this without the explicit passing of “private”.

    void user(void)
    {
        struct statement *s = make_a_statement();
    
        if (statement_analyse(s, foo))
            blah blah;
    }
    

    While it may seem that this provides no benefit, because you still have to implement the accessors, assuming that you want a well defined and robust interface, the accessor functions are the only sane place to put the assertions and the interface documentation. In fact, if you write good assertions, the assertions themselves help document the interface. And once you add sanity checks in the accessors, you don’t have to add them in the actual methods they call.

    Of course, this approach only makes sense when the function called via the function pointer will be something provided by the user, or in some other way can be different things. If there’s a single analyse() method that will always do the same thing, you can simply implement a statement_analyse() that directly does what it needs to do.

    Small note: when doing OOP, I prefer to typedef the structs and give them CamelCase names. I use this convention as a way of telling that the struct is opaque and should only be accessed via its public interface. It also looks nicer, though that is subjective. I also prefer having the user allocate the memory for the struct itself, as opposed to the constructor malloc’ing it. That avoids having to handle malloc failure, and makes the program a little bit more efficient.

    typedef struct {
        ...
    } Statement;
    
    void Statement_Init (Statement *o);
    int Statement_Analyse (Statement *o, int pc);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to print some objects in a table having 2 rows per object,
Imagine you want to animate some object on a WinForm. You setup a timer
I want to set some attributes just before the object is serialized, but as
I want to expose some functionality of a internal object as a DLL -
I want to change some properties of a LINQ query result object without creating
When we want to modify some value in one object we may use two
I want to use session object in my web app.I want to store some
If I want to tweak some of the capability of a jQuery UI object,
I'm a relatively newbie to object oriented programming in JavaScript, and I'm unsure of
I'm new to object-oriented programming, and I'm struggling to think of OOP. What I

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.