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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:41:34+00:00 2026-05-17T22:41:34+00:00

Possible Duplicate: Can you write object oriented code in C? Hi! Just for the

  • 0

Possible Duplicate:
Can you write object oriented code in C?

Hi!

Just for the fun of it, I’ve been experimenting these last two days with creating a very simple, very straightforward object environment in pure C. I’ve been toying around with macros, dynamic linking, type-description structures and the like, and I’ve arrived at the following:

string_o str = new(String, "hello world");
list_o list = new(List);

List.pushf(list, str);

printf("In the list: \"%s\"\n",
       String.text(List.popf(list)));

delete(list);
delete(str);

Looks and works kinda nice, but I can’t figure a way to fake instance methods. I can’t get past Class.function(instance), not without global macro replacements for function names, which defeats the purpose of encapsulation.

Again, this is an experiment, just for the challenge and the fun =). Can you guys help me figure out a way to do this? I don’t want to use additional preprocessing, only plain C and GCC macros.

edit> forgot to say — I don’t want each instance to contain the function pointers in its structure. That would give me method syntax alright, but it would mean that a 4-byte data object would have a dozen function pointers copied over to each instance. That’s kinda like cheating =P haha

Thanks in advance!

  • 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-17T22:41:35+00:00Added an answer on May 17, 2026 at 10:41 pm

    Object orientation in C is normally done with function pointers. That means a structure which contains not only the data for an instance but the functions to call as well.

    It’s the easiest way to do inheritance and polymorphism in C. By way of example, here’s an object-orientd communications example.

    It only has one method open but you can see how that differs for the TCP and HTML sub-classes. By having an initialisation routine which sets the a class-specific function, you get polymorphism.

    #include <stdio.h>
    
    // The top-level class.
    
    typedef struct _tCommClass {
        int (*open)(struct _tCommClass *self, char *fspec);
    } tCommClass;
    
    // Function for the TCP class.
    
    static int tcpOpen (tCommClass *tcp, char *fspec) {
        printf ("Opening TCP: %s\n", fspec);
        return 0;
    }
    static int tcpInit (tCommClass *tcp) {
        tcp->open = &tcpOpen;
        return 0;
    }
    
    // Function for the HTML class.
    
    static int htmlOpen (tCommClass *html, char *fspec) {
        printf ("Opening HTML: %s\n", fspec);
        return 0;
    }
    static int htmlInit (tCommClass *html) {
        html->open = &htmlOpen;
        return 0;
    }
    
    // Test program.
    
    int main (void) {
        int status;
        tCommClass commTcp, commHtml;
    
        // Same base class but initialized to different sub-classes.
        tcpInit (&commTcp);
        htmlInit (&commHtml);
    
        // Called in exactly the same manner.
    
        status = (commTcp.open)(&commTcp, "bigiron.box.com:5000");
        status = (commHtml.open)(&commHtml, "http://www.microsoft.com");
    
        return 0;
    }
    

    A more complete answer can be found here.

    In response to your comment:

    I don’t want the functions contained in every single instance.

    You’re probably right. It’s unnecessary to duplicate that information when it will be the same for every instance of a single class.

    There’s a simple way around that. Rather than having every instance carry its own set of function pointers, you create one structure holding them for the class, then each instance gets a pointer to that structure.

    That will save quite a bit of space at the (minimal) cost of having to do two levels of indirection to call a function.

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

Sidebar

Related Questions

Possible Duplicate: Can you write object oriented code in C? Hi, can someone point
Possible Duplicate: Unpythonic way of printing variables in Python? In PHP one can write:
Possible Duplicate: What regular expression can never match? how do i write a regular
Possible Duplicate: How can I use Delphi to test if a Directory is writeable?
Possible Duplicate: Assembler mov issue I have the next code: mov ax,@data mov ds,ax
Possible Duplicate: Learning to write a compiler I looked around trying to find out
Possible Duplicate: Image comparison algorithm So basically i need to write a program that
Possible Duplicate: Interface vs Abstract Class (general OO) Hi guys, I decided to dig
Possible Duplicate: 'Contains()' workaround using Linq to Entities? I am using the System.Linq.Dynamic library
Possible Duplicate: what is difference of $(function(){ }); and $(document).ready(function() { }) ;? What

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.