Re: Is c++ only better c ?
"James Kanze" <james.kanze@gmail.com> wrote in message
news:ded8588f-baa2-4229-a17e-b658a95105fc@34g2000hsh.googlegroups.com...
On Oct 24, 11:55 am, Maxim Yegorushkin <maxim.yegorush...@gmail.com>
wrote:
On Oct 24, 10:27 am, Pawel_Iks <pawel.labed...@gmail.com> wrote:
I've read somewhere that c++ is something more than better c
... then I talk with my friend and he claimed that c++ is
nothing more than better c ... I tried to explain him that
he was wrong but I forgot all arguments about it. Could
someone told something about it?
Some actually consider C++ to be worse than
C:http://esr.ibiblio.org/?p=532
You'll find some idiot to defend just about any position. (Not
that all people who are critical of C++ are idiots. But the
intelligent ones don't like C either; the real problem with C++
is that it inherits too much from C.)
C++ definitely improves C. It also adds a lot of things which
support idioms which aren't supported in C. I suppose that you
could call support for OO,
[...]
You can get "fairly clean" abstract interfaces in C; something as simple as;
quick code scribbling - may have typo:
IShape.h
--------------------------------------------
struct IShape_VTable {
void (*IObject_Destroy) (void*);
void (*IShape_Draw) (void*);
/* ect... */
};
struct IShape {
struct IShape_VTable* VTable;
};
#define IObject_Destroy(Self) ( \
(Self)->VTable->IObject_Destroy((Self)) \
)
#define IShape_Draw(self) ( \
(Self)->VTable->IShape_Draw((Self)) \
)
That all the infrastructure. Now to create actual shapes...
Circle.h
--------------------------------------------
extern struct IShape*
Circle_Create(
/* ... */
);
Circle.c
--------------------------------------------
#include "Circle.h"
#include <stdlib.h>
static void Circle_IObject_Destroy(void*);
static void Circle_IShape_Draw(void*);
static struct IShape_VTable Circle_VTable = {
Circle_IObject_Destroy,
Circle_IShape_Draw
};
struct Circle {
struct IShape IShape;
/* ... */
};
struct IShape*
Circle_Create(
/* ... */
) {
struct Circle* Self = malloc(*Self);
if (Self) {
Self->IShape.VTable = &Circle_VTable;
return &Self->IShape;
}
return NULL;
}
void
Circle_IObject_Destroy(
void* IObject
) {
free(IObject);
}
void
Circle_IShape_Draw(
void* IShape
) {
struct Circle* const Self = IShape;
/* ... */
}
Now, finally we can use the Circle via. the abstract interfaces IShape and
IObject:
main.c
--------------------------------------
#include "Circle.h"
int main(void) {
struct IShape* Shape = Circle_Create(/* ... */);
IShape_Draw(Shape);
IObject_Destroy(Shape);
return 0;
}
There... simple!
;^D