Re: Object-oriented programming in standard ANSI C
- From: rpbg123@xxxxxxxxx (Roland Pibinger)
- Date: Sun, 19 Nov 2006 12:16:08 GMT
On Sat, 18 Nov 2006 20:32:41 GMT, Keith Thompson wrote:
"E. Robert Tisdale" writes:
[...]
You probably meant "data hiding".
The C computer programming language allows you to encapsulate data
[in a struct] but it doesn't support data hiding at all.
There have been many discussions whether encapsulation and information
hiding are the same or whether one is a means for the other. Let's
assume we mean that data should not be accessible from the 'outside'.
It doesn't? At all?
Using an opaque data types
(publishing a struct declaration without publishing it's definition)
precludes some compile time optimizations.
Ah, so it *does* support data hiding.
I've always wondered why C programmers often unnecessarily give up
encapsulation. You can frequently see code like the following:
struct my_struct {
// some data
};
typedef struct my_struct my_struct;
my_struct* init (int i);
int do_something (my_struct* p, int n, float f);
// more functions
void cleanup (my_struct* p);
In the above example the my_struct definition in the header file is
unnecessary, a forward declaration (struct my_struct;) is sufficient.
The user not only gets access to 'private' data but also the my_struct
data become part of the published interface. This means that future
releases of the code cannot alter that data any more (not even
rearrange them) without potentially breaking existing code. Sometimes
the (generous) use of macros prevents encapsulation but in many cases
lack of encapsulation seems to be just an oversight of the programmer.
Best regards,
Roland Pibinger
.
- Follow-Ups:
- Re: Object-oriented programming in standard ANSI C
- From: Chris Thomasson
- Re: Object-oriented programming in standard ANSI C
- From: E. Robert Tisdale
- Re: Object-oriented programming in standard ANSI C
- References:
- Object-oriented programming in standard ANSI C
- From: Thierry Chappuis
- Re: Object-oriented programming in standard ANSI C
- From: E. Robert Tisdale
- Re: Object-oriented programming in standard ANSI C
- From: Roland Pibinger
- Re: Object-oriented programming in standard ANSI C
- From: E. Robert Tisdale
- Re: Object-oriented programming in standard ANSI C
- From: Roland Pibinger
- Re: Object-oriented programming in standard ANSI C
- From: E. Robert Tisdale
- Re: Object-oriented programming in standard ANSI C
- From: Keith Thompson
- Object-oriented programming in standard ANSI C
- Prev by Date: Re: when to use C and when to use C++
- Next by Date: Re: Overview
- Previous by thread: Re: Object-oriented programming in standard ANSI C
- Next by thread: Re: Object-oriented programming in standard ANSI C
- Index(es):
Relevant Pages
|