Abstraction in C

S

s0suk3

I'm developing a project which I'm trying to keep as organized as
possible. Since C doesn't support classes, I've been trying to make do
mostly with structs and functions that operate on them. However,
sometimes some of the "higher-level" modules contain functions that
map more or less directly to functions calls in "lower-level" modules.
So I've been having trouble naming these such functions, since ideally
both of them (the one in the higher-level module and the one in the
lower-level module) should have the same names. So, are there any
naming conventions for these cases? Or are there any alternatives for
doing what I'm doing?

Thanks,
Sebastian
 
F

Fred

I'm developing a project which I'm trying to keep as organized as
possible. Since C doesn't support classes, I've been trying to make do
mostly with structs and functions that operate on them. However,
sometimes some of the "higher-level" modules contain functions that
map more or less directly to functions calls in "lower-level" modules.
So I've been having trouble naming these such functions, since ideally
both of them (the one in the higher-level module and the one in the
lower-level module) should have the same names. So, are there any
naming conventions for these cases? Or are there any alternatives for
doing what I'm doing?

Add a short (one or two letter) prefix to all functions in the module.
This has the advantage of making it obvious where to find the code in
a multi-file project.

For exzmple, in Unix, the X11 library functions all start with "X",
the toolkit functions all start with "Xt", and Motif functions start
with "Xm".
 
R

rahul

However you can write emp_getname() and cust_getname() to take void *s
instead of the real structure type, and convert the parameter in the first
line. Then you can do fancy mapping and polymorphism.
Just be aware that C won't give you the benefits of type checking and
type safety. The compiler or C lang. won't stop you from passing you
an object of Employee to cust_getname(this may not fail as both the
structures may have a member named name). You will have to introduec
type checking yourself(that is, if you really need it).
 
F

Friedrich

I'm developing a project which I'm trying to keep as organized as
possible. Since C doesn't support classes, I've been trying to make do
mostly with structs and functions that operate on them. However,
sometimes some of the "higher-level" modules contain functions that
map more or less directly to functions calls in "lower-level" modules.
So I've been having trouble naming these such functions, since ideally
both of them (the one in the higher-level module and the one in the
lower-level module) should have the same names. So, are there any
naming conventions for these cases? Or are there any alternatives for
doing what I'm doing?
It does not support classes but structs so you can use them

just assume the following structs


struct Employee {
struct EmployeeFtable *funTable;
struct EmployeeData *dataTable;
};

struct EmployeeFtable {
int (*get_id)(struct Employee *);
int (*set_id)(struct Employee *, int nId);
}

struct EmployeeData {
int id;
char *first_name
char *last_name;
};


struct OthterStruct {
struct OtherStruct *funTable;
...

struct OtherStructFtable {
int (*get_id)(struct ....
};


You can use as many struct with get_id as you can...

Howerver you can use the same approach as glib-2 and will probably
gain quit a lot of it.

Regards
Friedrich
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top