On Nov 4, 12:24*pm, c...@tiac.net (Richard Harter) wrote:
[snip]
Quote:
Perhaps all of this is overkill or perhaps there is a much better
way. *Comments are welcome.
Err...
If you want OO, then use C++.
If you want procedural, then use C.
I think trying to use a butter knife as a screwdriver or a screwdriver
as a butterknife can be made to work. But it will be a lot easier if
you simply use the tools for what they were designed for.
A few years ago, it would be a much bigger issue than it is now,
because C compilers were ubiquitous and C++ compilers were less
common. But G++ is available even in embedded/cross compiler areas
now.
If there is the rare case where C++ is ruled out (for whatever reason)
and an object model is desirable, then I recommend fake C++:
// typedef Get methods:
typedef void (*fakeClassGet_data_int) (int );
typedef void (*fakeClassGet_data_str) (char[20]);
typedef void (*fakeClassGet_data_datetime) (struct_tm);
typedef void (*fakeClassGet_data_ptr) (void *);
// typedef Put methods:
typedef int (*fakeClassSet_data_int) (int );
typedef char* (*fakeClassSet_data_str) (char[20]);
typedef struct tm (*fakeClassSet_data_datetime) (struct_tm);
typedef void* (*fakeClassSet_data_ptr) (void *);
// typedef Constructor (You might also make it take all of the
// function pointers instead of assigning them):
typedef struct fakeClass* (*fakeClassConstructor) (size_t count);
// typedef destructor
typedef void (*fakeClassDestructor) (void);
typedef struct fakeClass {
// Am I a list?
size_t instance_count;
// Internal data members:
int data_int;
char[20] data_str ;
struct tm data_datetime;
void *data_ptr;
// Get Methods:
fakeClassGet_data_int Get_data_int;
fakeClassGet_data_str Get_data_str;
fakeClassGet_data_datetime Get_data_datetime;
fakeClassGet_data_ptr Get_data_ptr;
// Put methods:
fakeClassSet_data_int Set_data_int;
fakeClassSet_data_str Set_data_str;
fakeClassSet_data_datetime Set_data_datetime;
fakeClassSet_data_ptr Set_data_ptr;
// Constructor:
fakeClassConstructor Constructor;
// Destructor:
fakeClassDestructor Destructor;
};
Obviously, the constructors and destructors must be fired manually.
You will also have to make assignment methods instead of operators and
other pretty things like that.
In reality, I doubt if I would want to take on a project like that
{though I have done something like that in the distant past}. If
there were really no C++ compiler available, I would look to see if
there were some C-Front type solution if an object model were really
needed. Either that, or consider some other languages besides C and/
or C++.
Otherwise, when you are all done, someone is likely to say:
"Sure it turns the screw, but it looks a heck of a lot like a butter
knife!"