Connecting Tech Pros Worldwide Forums | Help | Site Map

Re: A question of programming technique in C

user923005
Guest
 
Posts: n/a
#1: Nov 4 '08
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!"

Pascal J. Bourguignon
Guest
 
Posts: n/a
#2: Nov 5 '08

re: Re: A question of programming technique in C


user923005 <dcorbit@connx.comwrites:
Quote:
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++:
I'd rather recommend fake Objective-C, if you don't want the
Objective-C pre-processor. Well actually it's true Objective-C, but
at the C level. The Objective-C runtime is available as a C library,
you can just use it directly from C programs, building at run-time your
classes and methods.
http://developer.apple.com/documenta...reference.html

It's more fun programming in a dynamic environment than a static one.

--
__Pascal Bourguignon__
Closed Thread