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
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?
... and there ain't no inheritance ...
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.
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.
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.
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.
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:
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.