Hello there, fellow programmers! I am an Aeronautical Engineering student who is doing research on Multidisciplinary Optimization (MDO) since the beginning of this year.

All the code I’ve written so far is in Python, but I want to move it to C so I can learn more about low level programming and so I can have more control over what my code actually does (since it is an MDO code, it needs to be very optimized because every 0.5 seconds added to each iteration add 500 seconds over 1000 iterations (9 minutes!)). I am taking an online course on C from edX, but I still cannot understand how to actually substitute objects for C compliant code. The main problem is that my original code uses lots of objects, and I simply do not know how to eliminate objects from my code.

Can someone help me? Any help is welcomed.

[edit] Thanks for all your answers, I think I’m getting it now.

  • Sea of Tranquility@beehaw.org
    link
    fedilink
    arrow-up
    5
    ·
    edit-2
    1 year ago

    There are a many approaches to implementing OOP in C. Since C doesn’t give you any language constructs to implement this out of the box, it’s up to you to do it in a consistent and understandable manner. Since there is no definite way to do it, let me just give you an example of how I would translate a Python file, and you can decide how you implement it from there:

    --------------
    class Parent:
        def __init__(self, param1: str, param2: int):
            self.__param1 = param1
            self.__param2 = param2
        def __private_method(self):
            print("private method")
        def public_method(self):
            print("public method")
        @staticmethod
        def static_method():
            print("static method")
        @property
        def param1(self):
            return self.__param1
    
    class Child(Parent):
        def __init__(self):
            super().__init__("param1", 2)
    --------------
    

    I would split the C code for this into header and source files:

    (header.h)

    --------------
    #pragma once
    
    /// Parent Class ///
    typedef struct Parent_obj {
        char* param1
        unsigned int param1_len
        int param2
    } Parent_obj_t;
    void Parent_init(Parent_obj_t* self, char* param1, unsigned int param1_len, int param2);
    void Parent_public_method(Parent_obj_t* self);
    void Parent_static_method();
    void Parent_param1(Parent_obj_t* self, char* out, unsigned int max_len); // property method with upper bound string length
    void Parent_del(Parent_obj_t* self); // destruct object (similar to __del__() in python)
    
    /// Child Class ///
    typedef struct Child_obj {
        Parent_hidden_state_t* super
        char* param
        unsigned int param_len
    } Child_obj_t
    void Child_init(Child_obj_t* self, Parent_obj_t* super);
    void Child_del(Child_obj_t* self);
    --------------
    

    (source.c)

    --------------
    #include "header.h"
    
    /// Parent Class ///
    // private methods
    void Parent_private_method(Parent_obj){...} // not visible in the header file
    // public methods
    void Parent_init(Parent_obj_t* self, char* param1, unsigned int param1_len, int param2){...}
    void Parent_public_method(Parent_obj_t* self){...}
    void Parent_static_method(){...}
    void Parent_param1(Parent_obj_t* self, char* out, unsigned int max_len){...}
    void Parent_del(Parent_obj_t* self){...}
    
    /// Child Class ///
    // public methods
    void Child_init(Child_obj_t* self, Parent_obj_t* super){...}
    void Child_del(Child_obj_t* self){...}
    --------------
    

    Modules and namespaces can be modeled using folders and prefixing your structs and functions.

    • raubarno@lemmy.ml
      link
      fedilink
      arrow-up
      1
      ·
      1 year ago

      This. Although I would argue it is a bad practice inherit from a class, only from an interface. GObject is your friend.