LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Encapsulation and information hiding with pure C language (https://www.linuxquestions.org/questions/programming-9/encapsulation-and-information-hiding-with-pure-c-language-4175466777/)

shifter 06-20-2013 03:00 PM

Encapsulation and information hiding with pure C language
 
In what way is it possible to use encapsulation and information hiding principles with C language? (without considering Object Oriented paradigm and C++)

NevemTeve 06-20-2013 03:14 PM

A well-known method for opaque data-types is the following:
Code:

/* somestuff.h */

typedef struct Stuff Stuff;

extern Stuff *StuffConstruct (int param);
extern void StuffDestruct (Stuff *);

... other methods ...

Code:

/* somestuff.c */

#include "somestuff.h"

struct Stuff {
...
};

Stuff *StuffConstruct (int param)
{
    Stuff *ptr= malloc (sizeof (*ptr));
    ...
    return ptr;
}

void StuffDestruct (Stuff *ptr)
{
    ...
    free (ptr);
}

... other methods ...


shifter 06-20-2013 04:17 PM

Why did you declare functions as "extern" in header file?

ta0kira 06-20-2013 09:48 PM

It isn't necessary in this case, but it's often used to indicate that the function is going to be defined in another file (as opposed to later in the same file.) On the other hand, you would need to use extern for a global variable made available by the header to make it a declaration rather than a definition.

Kevin Barry

NevemTeve 06-21-2013 03:53 AM

Everything else is clear?

shifter 06-21-2013 04:29 AM

Ok, now everything is clear, thank you very much!


All times are GMT -5. The time now is 07:03 PM.