Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

OOP approach in C

Status
Not open for further replies.

BellyTelly

Programmer
Jul 24, 2001
2
0
0
DE
Hi folks,

following issue concerns C only:

due to the fact that many global variables in our project leads to many problems (unpredictable side-effects) I want to isolate them. That is, I want to create an extra file where I group some global variables decorating them with static attribute. Besides, I create some functions which have the unique right to alter the global variables.

At a thight view it looks like OOP, as I give the files names like I would give them classes. By this way I can separate concerns and assure that no dumb function can directly alter the state of global variables.

In order to have a more precise idea what I mean here is an example:

file status.c:

#define UP_LIMIT 25
#define LO_LIMIT 3
#define SET_ERROR -100

static int vi_status;

int getVi_Status()
{
return (vi_status);
}

int setVi_Status(int new_status)
{
if (new_status > LO_LIMIT && new_status < UP_LIMIT)
{
vi_status = new_status;
}
else
return (SET_ERROR);
}

-----------

file myproject.c:

//Here I can alter the state of vi_status by get-/setVi_Status only. In addtion, the use of these functions apparently shows a modification of global vi_status.

Does anybody agree with me or has got another approach hiding his/her global variables?

Any hint is highly appreciated.

Yours BellyTelly





 
Hi,

sorry for not putting the code in the appropriate environment. I haven't been here for ages. Looking up for help here how to post code didn't succeed.

Can anyone tell me how I post code here in a right manner, please.

THX in advance.
 
well.. to post code, you should use code tags:

put [ code] just before your code and [ /code] right after. i have put a space in the tags so that everything between my tags doens't show up as code.. so you will have to remoe all the spaces from those tags..

as for your question.. have you considered using a struct? i havne't used c much.. just an idea...
 
when you preview your post checkout the code tags at the bottom of the "editing tips" section. I can't seem to figure out how to print the tag literals in my post, sorry.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top