A
Alberto =?iso-8859-1?Q?Gim=E9nez?=
Hi, I've seen some object oriented programming bits out there and i'm
not sure if they're legal. For example:
struct Object {
int field1;
int field2;
};
struct SubObject {
int field1; /* the same as Object */
int field2; /* the same as Object */
int subobject_field1;
int subobject_field2;
};
And a cast is used to reference the "superclass" of SubObject:
struct SubObject *subobject;
struct Object *parent = (Object *) subobject; /* legal cast? */
And then use parent->field1, etc. Is that cast legal? invokes UB or
something worse?
I've also seen some other code, whichi IMHO is more correct and elegant,
which is kind a real framework for OOP in ANSI C:
struct Class {
size_t size;
int (*ctor) (... etc)
};
struct String {
struct Class *class;
char *text;
};
And so on. Of course, is uses clever new() and so functions.
I've seen it in a pdf book, but I can't tell you the title (the pdf has
not titlepage itself).
Thanks and greetings.
not sure if they're legal. For example:
struct Object {
int field1;
int field2;
};
struct SubObject {
int field1; /* the same as Object */
int field2; /* the same as Object */
int subobject_field1;
int subobject_field2;
};
And a cast is used to reference the "superclass" of SubObject:
struct SubObject *subobject;
struct Object *parent = (Object *) subobject; /* legal cast? */
And then use parent->field1, etc. Is that cast legal? invokes UB or
something worse?
I've also seen some other code, whichi IMHO is more correct and elegant,
which is kind a real framework for OOP in ANSI C:
struct Class {
size_t size;
int (*ctor) (... etc)
};
struct String {
struct Class *class;
char *text;
};
And so on. Of course, is uses clever new() and so functions.
I've seen it in a pdf book, but I can't tell you the title (the pdf has
not titlepage itself).
Thanks and greetings.