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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:45:27+00:00 2026-06-14T20:45:27+00:00

gcc (GCC) 4.7.2 c89 I am doing some practicing on using some object-oriented style

  • 0
gcc (GCC) 4.7.2
c89

I am doing some practicing on using some object-oriented style in C. As I want to do this for some of our larger projects.

However, in my code below I have a parent structure car and a child sports_cars. However, all the commom attibutes or the car struct will be present in the child structures.

There is one question I have and that is the init and destory functions. As they are commom I want my child structures to heritant the init and the destroy. But I think I am doing it wrong.

car->init = init_car;

As I have the init function pointer pointing to init_car, doesn’t look right to me.

Many thanks for any suggestions,

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

typedef struct tag_car car_t;
struct tag_car {
    size_t wheels;
    char *name;

    void (*init)(void *self);
    void (*destroy)(void *self);
    size_t (*wheels_get)(void *self);
    void (*wheels_set)(void *self, size_t num_wheels);
};

typedef struct tag_sports sports_t;
struct tag_sports {
    car_t base_car;

    size_t top_speed;
    size_t (*top_speed_get)(void *self);
    void (*top_speed_set)(void *self, size_t max_top_speed);
};

void destroy_car(void *self)
{
    car_t *car = self;

    free(car);
}

void init_car(void *self)
{
    car_t *car = car;

    car->wheels = 4;
    car->name = NULL;

    car->init = init_car;
    car->destroy = destroy_car;

}

size_t wheels_count_get(void *self)
{
    car_t *car = self;

    return car->wheels;
}

void wheels_count_set(void *self, size_t num_wheels)
{
    car_t *car = self;

    car->wheels = num_wheels;
}

size_t sports_top_speed_get(void *self)
{
    sports_t *sports_car = self;

    return sports_car->top_speed;
}

void sports_top_speed_set(void *self, size_t max_top_speed)
{
    sports_t *sports_car = self;

    sports_car->top_speed = max_top_speed;
}

sports_t* init_sports()
{
    sports_t *sports_car = malloc(sizeof(sports_t));

    /* Parent struct */
    sports_car->base_car.init = init_car; 
    sports_car->base_car.destroy = destroy_car;
    sports_car->base_car.wheels_get = wheels_count_get;
    sports_car->base_car.wheels_set = wheels_count_set;

    /* Child struct */
    sports_car->top_speed_set = sports_top_speed_set;
    sports_car->top_speed_get = sports_top_speed_get;

    return sports_car;
}

int main(void)
{
    sports_t *farreri = init_sports();
    sports_t *lamborghini = init_sports();

    farreri->base_car.wheels_set(farreri, 10);
    farreri->top_speed_set(farreri, 240);

    printf("farreri has wheel count [ %ld ]\n", farreri->base_car.wheels_get(farreri));
    printf("Farreri has a top speed [ %ld ]\n", farreri->top_speed_get(farreri));

    lamborghini->base_car.wheels_set(lamborghini, 6);
    lamborghini->top_speed_set(lamborghini, 220);

    printf("lamborghini has wheel count [ %ld ]\n", lamborghini->base_car.wheels_get(lamborghini));
    printf("Lamborghini has a top speed [ %ld ]\n", lamborghini->top_speed_get(lamborghini));

    farreri->base_car.destroy(farreri);
    lamborghini->base_car.destroy(lamborghini);

    return 0;
}
  • 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-14T20:45:28+00:00Added an answer on June 14, 2026 at 8:45 pm

    In your init_car the lines

    car->init = init_car;
    car->destroy = destroy_car;
    

    shouldn’t be present. You’re mixing the construction behavior with initialization. In C++ the constructor is responsible for doing both; if you want to simulate the same behavior, you should expose allocation interface (e.g., call it construct) which will allocate the memory (your malloc and also setting the object “methods” to the appropriate functions) and initialization interface (which will do the init stuff: car->wheels = 4; car->name = NULL;). The allocation method can call the initialization method to achieve the C++ behavior. You’ll have something like this:

    void init_car(void *self)
    {
        car_t *car = self;
    
        car->wheels = 4;
        car->name = NULL;
    }
    
    sports_t* construct()
    {
        sports_t *sports_car = malloc(sizeof(sports_t));
    
        /* Set the methods */
        sports_car->base_car.init = init_car; 
        sports_car->base_car.destroy = destroy_car;
        sports_car->base_car.wheels_get = wheels_count_get;
        sports_car->base_car.wheels_set = wheels_count_set;
    
        /* initialize the object */
        sports_car->base_car.init();
    
        return sports_car;
     }
    
    int main(void)
    {
        /* construct and init the objects */
        sports_t *ferrari = construct();
        sports_t *lamborghini = construct();
    
        /* do your manipulations */
    
        /* destroy the objects */
        ferrari->base_car.destroy(farreri);
        lamborghini->base_car.destroy(lamborghini);
    
        return 0;
    }
    

    If you don’t want the construction to init the object, just don’t invoke the initialization from construct, invoke it after the construction by calling sports_car->base_car.init.

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

Sidebar

Related Questions

gcc 4.5.1 c89 I am trying to free some memory. However, when I check
gcc 4.4.2 c89 I re-engineering some code in c89. However, I am totally confused
gcc c89 I am getting a stack dump on this line: strcpy(comp->persons->name, Joe); However,
gcc 4.4.2 c89 I was just working on some pointers. However, with the program
gcc (GCC) 4.6.3 c89 Hello, I am just wondering if this is the best
gcc (GCC) 4.6.3 c89 I am trying to use usleep . However, I keep
gcc 4.4.3 c89 I am just getting started with log4c. However, there is very
gcc 4.6.2 c89 I have the following 2D array that I want to pass
c89 gcc (GCC) 4.7.2 Hello, I am looking at some string functions as I
gcc 4.6.2 c89 I am just wondering if this is a good way to

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.