How to structure a C program effectively

后端 未结 7 1326
独厮守ぢ
独厮守ぢ 2020-12-24 12:01

Let me first say that I\'ve got a fair amount of experience in both C and C++. However, I\'m starting a new project in C and I\'ve been working in object-oriented languages

相关标签:
7条回答
  • 2020-12-24 12:22

    Hmmm... We used to just use naming conventions... Ergo: str* does stuff with what common datastructure? So maybe just take the C# syntax and s/./_/g?

    • foo_constructor
    • foo_destructor
    • foo_someMethod
    • foo_someMethod2 // ain't no overloading in ANSI C
    • foo_otherMethod

    ... and there ain't no inheritance ...

    • foo2_constructor
    • foo2_destructor
    • foo2_someMethod // and there ain't no polymorphism

    But look on the bright side... you can use pointer-to-pointer-to-pointer-to-function-returning-a-pointer-to-pointer-int! Oh the joy!

    My bestest advise is to learn the lessons of Java (and by inference C#) and structure your libraries to NOT have side-effects... more typdefs == less headaches... and if your work-out how to follow this sage advise please let me know ;-)

    Cheers. Keith.

    0 讨论(0)
  • 2020-12-24 12:29

    C has been a low-level language and in the respect it would be very useful to organize your data structures in accordance with your code functions and modules.

    I would suggest that you use typedefs and enumerations wherever you would like to create data objects. Use macros or static functions to initialize, allocate and 'destroy' as required.

    0 讨论(0)
  • 2020-12-24 12:31

    I have been working on a project for a little while where the library needs to be in C, but I want to have some form of OO functionality. I am doing something similar to this with a little more detail.

    struct klass {
      char * value;
    
      void (*set_value) (struct klass *, const char *);
      void (*destroy) (struct klass *);
    };
    
    static void
    klass_method_set_value (struct klass * k, const char * value) {
      if (k->value == NULL) {
      }
    }
    
    static void 
    klass_object_desetroy (struct klass * k) {
      free (k);
      k = NULL;
    }
    
    static void
    klass_method_destroy (struct klass * k) {
      klass_object_destroy (k);
    }
    
    static struct klass *
    klass_object_init (void) {
      struct klass * obj = (struct klass *) malloc (sizeof (struct klass*) );
    
      /* members */
      obj->value = NULL;
    
      /* methods */
      obj->set_value = klass_method_set_value;
      obj->destroy = klass_method_destroy;
      return obj;
    }
    
    struct klass * 
    klass_new (void) {
      return klass_object_init ();
    }
    

    Forgive me if something is wrong; wrote it a little quick.

    0 讨论(0)
  • 2020-12-24 12:37

    This is quite a normal and sensible practice. But try not to expose the struct layout in header files, so that you have some flexibility in how it's implemented and manage your dependencies better.

    See Opaque pointer for more details.

    0 讨论(0)
  • 2020-12-24 12:37

    That's a pretty reasonable way to write a C program. There is another large application out there, which does pretty much the same stuff - called the Linux kernel. Some nearly OO-features used in there:

    • structs and operations on structs for encapsulation just like in your example
    • pointers to base structs as a form of poor man's inheritance -- you'll find loads of references to struct kobject in there
    • macros to generate functions as a replacement for template programming
    0 讨论(0)
  • 2020-12-24 12:41

    I agree with the suggestions above. You are doing it the best way .. if you want to program in C.

    Of course, you could write a pre-processor to automatically generate these declarations and things for you .. maybe use a "Class" declaration ... put the functions you want to be member functions inside the class .. etc.

    But what we've got here is a simple C++ to C compiler. Why not just program in C++, use a real C++ compiler, use clean interfaces, and just link the C++ code with the C code? What is the reason that you need to code in C vs. C++ anyways? Or if you need to, generate C code from the compiler and compile the output C code together with whatever else you need.

    0 讨论(0)
提交回复
热议问题