{"id":334946,"date":"2022-06-24T15:00:30","date_gmt":"2022-06-24T15:00:30","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=334946"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=334946","title":{"rendered":"<span>\u0410\u0431\u0441\u0442\u0440\u0430\u043a\u0446\u0438\u0438 \u0438 \u043d\u0430\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u0435 \u0432 \u0421\u0438 \u2014 \u0441\u0442\u0440\u0435\u043b\u044f\u0435\u043c \u043f\u043e \u043d\u043e\u0433\u0430\u043c \u043a\u0440\u0430\u0441\u0438\u0432\u043e<\/span>"},"content":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<blockquote>\n<p><sup>TL;DR<\/sup> <a href=\"https:\/\/github.com\/pomidoroshev\/c-inheritance\" rel=\"noopener noreferrer nofollow\">https:\/\/github.com\/pomidoroshev\/c-inheritance<\/a><\/p>\n<\/blockquote>\n<p>\u0418\u043d\u043e\u0433\u0434\u0430 \u043d\u0435\u0442-\u043d\u0435\u0442 \u0434\u0430 \u0438 \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u0447\u0442\u043e-\u043d\u0438\u0431\u0443\u0434\u044c \u0430\u0431\u0441\u0442\u0440\u0430\u0433\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438 \u043e\u0431\u043e\u0431\u0449\u0438\u0442\u044c \u0432  \u043a\u043e\u0434\u0435 \u043d\u0430 \u0421\u0438. \u041a \u043f\u0440\u0438\u043c\u0435\u0440\u0443, \u0445\u043e\u0447\u0435\u0448\u044c \u0442\u044b \u043f\u0440\u0438\u043d\u0442\u0430\u043d\u0443\u0442\u044c \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b  \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0440\u0430\u0437, \u043f\u0438\u0448\u0435\u0448\u044c \u0432\u0435\u0437\u0434\u0435, \u043a\u0430\u043a \u0434\u0443\u0440\u0430\u043a, <code>printf(\"%s %d %f\\n\", foo->bar, foo->baz, foo->boom)<\/code>, \u0438 \u0438\u043d\u0442\u0443\u0438\u0442\u0438\u0432\u043d\u043e \u043a\u0430\u0436\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u0435\u0441\u0442\u044c \u0441\u043f\u043e\u0441\u043e\u0431 \u0441\u0434\u0435\u043b\u0430\u0442\u044c <code>foo->print(foo)<\/code>, \u0438 \u0442\u0430\u043a \u0432\u043e\u043e\u0431\u0449\u0435 \u0441\u043e \u0432\u0441\u0435\u043c\u0438 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430\u043c\u0438, \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0441 <code>foo<\/code>.<\/p>\n<p>\u0412\u043e\u0437\u044c\u043c\u0435\u043c \u043f\u0440\u0438\u043c\u0435\u0440: \u0435\u0441\u0442\u044c \u043d\u0435\u043a\u0438\u0439 \u0447\u0443\u0432\u0430\u043a \u0441 \u0438\u043c\u0435\u043d\u0435\u043c \u0438 \u0444\u0430\u043c\u0438\u043b\u0438\u0435\u0439, \u0438 \u0435\u0441\u0442\u044c \u043f\u0442\u0438\u0446\u0430, \u0443 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0435\u0441\u0442\u044c \u0438\u043c\u044f \u0438 \u0432\u043b\u0430\u0434\u0435\u043b\u0435\u0446.<\/p>\n<pre><code class=\"cpp\">typedef struct Person Person; struct Person {     char *first_name;     char *last_name; };  typedef struct Bird Bird; struct Bird {     char *name;     Person *owner; };<\/code><\/pre>\n<p>\u0427\u0442\u043e\u0431\u044b \u0432\u044b\u0432\u0435\u0441\u0442\u0438 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043f\u0440\u043e \u044d\u0442\u0438\u0445 \u0436\u0438\u0432\u043e\u0442\u043d\u044b\u0445, \u043a\u043e\u043d\u0434\u043e\u0432\u044b\u0439 \u0441\u0438\u0448\u043d\u0438\u043a \u043d\u0430\u043f\u0438\u0448\u0435\u0442 \u043f\u0440\u043e\u0441\u0442\u043e \u0434\u0432\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438:<\/p>\n<pre><code class=\"cpp\">void Person_Print(Person *p) {     printf(\"%s %s\\n\", p->first_name, p->last_name); }  void Bird_Print(Bird *b) {     printf(\"%s of %s %s\\n\", b->name, b->owner->first_name, b->owner->last_name); }<\/code><\/pre>\n<p>\u0418 \u0431\u0443\u0434\u0435\u0442 \u0442\u0430\u043a\u0438 \u043f\u0440\u0430\u0432! \u041d\u043e \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0445  \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440 \u0443 \u043d\u0430\u0441 \u043c\u043d\u043e\u0433\u043e, \u0430 \u043d\u0430\u0448 \u043c\u043e\u0437\u0433 \u0438\u0441\u043f\u043e\u0440\u0447\u0435\u043d \u0432\u0435\u044f\u043d\u0438\u044f\u043c\u0438 \u041e\u041e\u041f? \u041f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e, \u043d\u0430\u0434\u043e \u0443  \u043a\u0430\u0436\u0434\u043e\u0439 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u043e\u0431\u0449\u0438\u0439 \u043c\u0435\u0442\u043e\u0434, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 <code>void Repr(Person* person, char* buf)<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u0431\u0440\u043e\u0441\u0438\u0442 \u0432 <code>buf<\/code>  \u0441\u0442\u0440\u043e\u043a\u043e\u0432\u043e\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 (\u0434\u0430, \u0442\u0435\u043f\u0435\u0440\u044c \u0443 \u043d\u0430\u0441 \u043f\u043e\u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u044b), \u0438  \u0434\u0430\u043b\u044c\u0448\u0435 \u043c\u044b \u0431\u044b \u043c\u043e\u0433\u043b\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u043e\u0442 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0434\u043b\u044f \u0432\u044b\u0432\u043e\u0434\u0430 \u043d\u0430 \u044d\u043a\u0440\u0430\u043d:<\/p>\n<pre><code class=\"cpp\">\/* Person *\/ struct Person {     void (*Repr)(Person*, char*);     \/* ... *\/ };  void Person_Repr(Person *person, char *buf) {     sprintf(buf, \"&lt;Person: first_name='%s' last_name='%s'>\",             person->first_name, person->last_name); }  Person *New_Person(char *first_name, char *last_name) {     Person *person = malloc(sizeof(Person));     person->Repr = Person_Repr;     person->first_name = first_name;     person->last_name = last_name;     return person; }  \/* Bird *\/ struct Bird {     void (*Repr)(Bird*, char*);     \/* ... *\/ };  void Bird_Repr(Bird *bird, char* buf) {     char owner_repr[80];     bird->owner->Repr(bird->owner, owner_repr);     sprintf(buf, \"&lt;Bird: name='%s' owner=%s>\",             bird->name, owner_repr); }  Bird *New_Bird(char *name, Person *owner) {     Bird *bird = malloc(sizeof(Bird));     bird->Repr = Bird_Repr;     bird->name = name;     bird->owner = owner;     return bird; }<\/code><\/pre>\n<p>\u041e\u043a\u0435\u0439, \u0432\u0440\u043e\u0434\u0435 \u0443\u043d\u0438\u0444\u0438\u0446\u0438\u0440\u043e\u0432\u0430\u043b\u0438, \u0434\u0430 \u043d\u0435 \u043e\u0447\u0435\u043d\u044c.  \u041a\u0430\u043a \u0442\u0435\u043f\u0435\u0440\u044c \u044d\u0442\u0438 \u043c\u0435\u0442\u043e\u0434\u044b \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c? \u041d\u0435 \u043e\u0447\u0435\u043d\u044c \u0443\u0434\u043e\u0431\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f, \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437  \u0432\u044b\u043b\u0435\u0437\u0430\u0435\u0442 \u0441\u0432\u0438\u0441\u0442\u043e\u043f\u043b\u044f\u0441\u043a\u0430 \u0441 \u0431\u0443\u0444\u0435\u0440\u0430\u043c\u0438:<\/p>\n<pre><code class=\"cpp\">char buf[80]; bird->Repr(bird, buf); printf(\"%s\\n\", buf);<\/code><\/pre>\n<p>\u041a\u0430\u043a \u0432\u0430\u0440\u0438\u0430\u043d\u0442 &#8212; \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0431\u0430\u0437\u043e\u0432\u0443\u044e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 <code>Object<\/code>, \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0432 \u043d\u0435\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u044e <code>Print()<\/code>, &#171;\u043d\u0430\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u044c&#187; \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u043e\u0442 <code>Object<\/code> \u0438 \u0432 <code>Object::Print()<\/code> \u0434\u0435\u0440\u0433\u0430\u0442\u044c \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u043c\u0435\u0442\u043e\u0434 <code>Repr()<\/code>. \u0412\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043b\u043e\u0433\u0438\u0447\u043d\u043e, \u0442\u043e\u043b\u044c\u043a\u043e \u043c\u044b \u043f\u0438\u0448\u0435\u043c \u043d\u0430 \u0421\u0438, \u0430 \u043d\u0435 \u043d\u0430 \u043f\u043b\u044e\u0441\u0430\u0445, \u0433\u0434\u0435 \u0442\u0430\u043a\u043e\u0435 \u043d\u0430 \u0440\u0430\u0437-\u0434\u0432\u0430 \u0440\u0435\u0448\u0430\u0435\u0442\u0441\u044f \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u043c\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u043c\u0438.<\/p>\n<p>\u041d\u043e \u0432 \u0421\u0438 \u0435\u0441\u0442\u044c \u0442\u0430\u043a\u0430\u044f \u0448\u0442\u0443\u043a\u0430: \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0438\u0432\u0435\u0441\u0442\u0438 \u043e\u0434\u043d\u0443 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u043a \u0434\u0440\u0443\u0433\u043e\u0439, \u0435\u0441\u043b\u0438 \u0443 \u043d\u0435\u0435 \u0442\u0430 \u0434\u0440\u0443\u0433\u0430\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0438\u0434\u0435\u0442 \u043f\u0435\u0440\u0432\u044b\u043c \u043f\u043e\u043b\u0435\u043c.<\/p>\n<p>\u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"cpp\">typedef struct {     int i; } Foo;  typedef struct {     Foo foo;     int j; } Bar;  Bar *bar = malloc(sizeof(Bar)); bar->foo.i = 123; printf(\"%d\\n\", ((Foo*)bar)->i);<\/code><\/pre>\n<p>\u0422\u043e \u0435\u0441\u0442\u044c \u043c\u044b \u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 <code>bar<\/code>, \u043d\u043e \u0441 \u0442\u0438\u043f\u043e\u043c <code>Foo<\/code>, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043f\u043e \u0441\u0443\u0442\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 &#8212; \u044d\u0442\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u0435\u0435 \u043f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442, \u0438 \u0442\u0443\u0442 \u043c\u044b \u0438\u043c\u0435\u0435\u043c \u043f\u0440\u0430\u0432\u043e \u0442\u0430\u043a \u043a\u0430\u0441\u0442\u043e\u0432\u0430\u0442\u044c.<\/p>\n<p>\u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0431\u0430\u0437\u043e\u0432\u0443\u044e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 <code>Object<\/code> \u0441 \u043e\u0434\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0435\u0439 <code>Print_Repr()<\/code>, \u043a\u043e\u0442\u043e\u0440\u0430\u044f, \u043f\u043e \u0438\u0434\u0435\u0435, \u0434\u043e\u043b\u0436\u043d\u0430 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0437\u0432\u0430\u0442\u044c &#171;\u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u043c\u0435\u0442\u043e\u0434&#187; <code>Repr()<\/code> \u0443 \u043d\u0430\u0448\u0438\u0445 \u043b\u044e\u0434\u0438\u0448\u0435\u043a \u0438 \u043f\u0442\u0438\u0447\u0435\u043a:<\/p>\n<pre><code class=\"cpp\">typedef struct Object Object; struct Object {     void (*Print_Repr)(Object*); };  \/*  \u0421\u0430\u043c\u0430\u044f \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u0430\u044f \u0447\u0430\u0441\u0442\u044c. \u0424\u0443\u043d\u043a\u0446\u0438\u044f \u0431\u0435\u0440\u0435\u0442 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c  \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435 \u043f\u043e\u043b\u0435 \u0432 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0435 \u043f\u043e\u0441\u043b\u0435 Object,  \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0432 \u0442\u0435\u043a\u0443\u0449\u0435\u043c \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0435\u043c  \u043d\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044e Repr().  *\/ void Object_Print_Repr(Object *object) {     void **p_repr_func = (void*) object + sizeof(Object);     void (*repr_func)(Object*, char*) = *p_repr_func;     char buf[80];     repr_func(object, buf);     printf(\"%s\\n\", buf); }  \/* Person *\/ typedef struct Person Person; struct Person {     Object object;     void (*Repr)(Person*, char*);     \/* ... *\/ };  Person *New_Person(char *first_name, char *last_name) {     Person *person = malloc(sizeof(Person));     person->object.Print_Repr = Object_Print_Repr;     person->Repr = Person_Repr;     \/* ... *\/     return person; }  \/* Bird *\/ typedef struct Bird Bird; struct Bird {     Object object;     void (*Repr)(Bird*, char*);     \/* ... *\/ };  Bird *New_Bird(char *name, Person *owner) {     Bird *bird = malloc(sizeof(Bird));     bird->object.Print_Repr = Object_Print_Repr;     bird->Repr = Bird_Repr;     \/* ... *\/     return bird; }<\/code><\/pre>\n<p>\u0412\u043e\u0442 \u043c\u044b \u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043b\u0438 \u043f\u0430\u0442\u0442\u0435\u0440\u043d &#171;\u0428\u0430\u0431\u043b\u043e\u043d\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434&#187; \u043d\u0430 \u0447\u0438\u0441\u0442\u043e\u043c \u0421\u0438. \u041d\u0435 \u0441\u043e\u0432\u0441\u0435\u043c \u0447\u0435\u0441\u0442\u043d\u043e, \u0438 \u043d\u0435 \u0441\u043e\u0432\u0441\u0435\u043c \u043d\u0430\u0434\u0435\u0436\u043d\u043e, \u043d\u043e \u043a\u043e\u0435-\u043a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442.<\/p>\n<p>\u0422\u0443\u0442 \u0434\u0432\u0430 \u0432\u043e\u043f\u0440\u043e\u0441\u0430:<\/p>\n<ol>\n<li>\n<p>\u041a\u0430\u043a \u0431\u044b\u0442\u044c, \u0435\u0441\u043b\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u044f <code>Repr()<\/code> \u043d\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0432\u0442\u043e\u0440\u044b\u043c \u043f\u043e\u043b\u0435\u043c \u0432 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0435?<\/p>\n<\/li>\n<li>\n<p>\u041a\u0430\u043a \u0431\u044b\u0442\u044c, \u0435\u0441\u043b\u0438 \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0438 \u0431\u043e\u043b\u0435\u0435 \u0447\u0435\u043c \u043e\u0434\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438?<\/p>\n<\/li>\n<\/ol>\n<p>\u041e\u0442\u0432\u0435\u0442 \u043d\u0435 \u0441\u0430\u043c\u044b\u0439 \u043f\u0440\u0438\u044f\u0442\u043d\u044b\u0439, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043f\u043e\u0440\u0442\u0438\u0442 \u0432\u0441\u044e \u043a\u0440\u0430\u0441\u043e\u0442\u0443 \u0438 \u0447\u0438\u0441\u0442\u043e\u0442\u0443 \u0431\u0430\u0437\u043e\u0432\u043e\u0439 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b <code>Object<\/code>, \u0442\u0443\u0434\u0430 \u043d\u0430\u0434\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0430\u0434\u0440\u0435\u0441\u0430 \u043d\u0443\u0436\u043d\u044b\u0445 \u043d\u0430\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u0439. \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u0445 \u043d\u0435\u0441\u043b\u043e\u0436\u043d\u043e, \u0432 <code>stddef.h<\/code> \u0435\u0441\u0442\u044c \u043f\u043e\u043b\u0435\u0437\u043d\u044b\u0439 \u043c\u0430\u043a\u0440\u043e\u0441 <code>offsetof(&lt;struct>, &lt;field>)<\/code>. \u0420\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043e\u043d \u0442\u0430\u043a:<\/p>\n<pre><code class=\"cpp\">struct A {     char c;     int i;     long l; }  offsetof(struct A, c) == 0; offsetof(struct A, i) == 4; offsetof(struct A, l) == 8;<\/code><\/pre>\n<p>\u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u044d\u0442\u043e\u0433\u043e \u043c\u0430\u043a\u0440\u043e\u0441\u0430 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043e\u0444\u0444\u0441\u0435\u0442\u044b \u0432\u0441\u0435\u0445 \u043d\u0443\u0436\u043d\u044b\u0445 generic-\u0444\u0443\u043d\u043a\u0446\u0438\u0439, \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0438\u0445 \u0432 <code>Object<\/code>, \u0438 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u0438\u0445 \u043e\u0442\u0442\u0443\u0434\u0430 \u0438\u0437 \u0434\u0440\u0443\u0433\u0438\u0445 \u043c\u0435\u0442\u043e\u0434\u043e\u0432. \u041a\u0440\u0430\u0441\u0438\u0432\u043e? \u0410 \u0442\u043e!<\/p>\n<p>\u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c, \u043a \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>Repr()<\/code> \u043c\u044b \u0437\u0430\u0445\u043e\u0442\u0435\u043b\u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e <code>Str()<\/code>, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u0442 \u043e\u0431\u044a\u0435\u043a\u0442 \u0432 \u0432\u0438\u0434\u0435 \u0441\u0442\u0440\u043e\u043a\u0438, \u043d\u043e \u0431\u0435\u0437 \u0432\u0441\u044f\u043a\u043e\u0439 \u0434\u0435\u0431\u0430\u0436\u043d\u043e\u0439 \u0448\u0435\u043b\u0443\u0445\u0438, \u0442\u0438\u043f\u0430 <code>&lt;Person first_name='Ivan' last_name='Ivanov'><\/code>, \u0430 \u043f\u0440\u043e\u0441\u0442\u043e \u0441\u0444\u043e\u0440\u043c\u0438\u0440\u0443\u0435\u0442 \u0441\u0442\u0440\u043e\u043a\u0443 <code>Ivan Ivanov<\/code> \u0434\u043b\u044f \u0432\u044b\u0432\u043e\u0434\u0430 \u0432 \u043a\u0430\u043a\u043e\u043c-\u0442\u043e \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0435. (\u0427\u0443\u0432\u0441\u0442\u0432\u0443\u0435\u0442\u0435 \u0432\u0435\u044f\u043d\u0438\u0435 Python \u0441 \u0435\u0433\u043e <code>__repr__()<\/code> \u0438 <code>__str__()<\/code>? \u041e\u043d\u043e \u0437\u0434\u0435\u0441\u044c \u043d\u0435 \u043f\u0440\u043e\u0441\u0442\u043e \u0442\u0430\u043a, <s>\u0430 \u0441\u043b\u043e\u0436\u043d\u043e \u0442\u0430\u043a<\/s>.)<\/p>\n<p>\u0421\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e, <code>Object<\/code> \u0434\u043e\u043b\u0436\u0435\u043d \u0438\u043c\u0435\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e <code>Print_Str()<\/code> \u0434\u043b\u044f \u0432\u044b\u0432\u043e\u0434\u0430 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432. \u0410 \u0447\u0442\u043e\u0431\u044b \u043e\u043d \u0446\u0435\u043f\u043b\u044f\u043b \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u043d\u0443\u0436\u043d\u043e \u0432\u043d\u0443\u0442\u0440\u0438 \u043d\u0435\u0433\u043e \u043f\u0440\u0438\u043a\u043e\u043f\u0430\u0442\u044c \u0432\u0441\u0435 \u043e\u0444\u0444\u0441\u0435\u0442\u044b.<\/p>\n<p>\u041b\u0438\u0441\u0442\u0438\u043d\u0433 \u0431\u0443\u0434\u0435\u0442 \u0431\u043e\u043b\u044c\u0448\u0435 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0445, \u0441 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u043c\u0438, \u043d\u043e \u0432\u044b \u043d\u0435 \u0431\u043e\u0439\u0442\u0435\u0441\u044c, \u043c\u044b \u0432\u0441\u0435 \u044d\u0442\u043e \u0441\u043a\u043e\u0440\u043e \u043f\u043e\u0440\u0435\u0444\u0430\u043a\u0442\u043e\u0440\u0438\u043c.<\/p>\n<pre><code class=\"cpp\">#include &lt;stdio.h> #include &lt;stdlib.h> #include &lt;stddef.h>  typedef struct Object Object; typedef struct Person Person; typedef struct Bird Bird;  struct Object {     size_t offset_repr;     void (*Print_Repr)(Object*);      size_t offset_str;     void (*Print_Str)(Object*); };  \/*  \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043f\u043e \u0430\u0434\u0440\u0435\u0441\u0443 object + offset_repr,  \u043a\u0430\u0441\u0442\u0430\u043d\u0443\u0442\u044c \u0435\u0435 \u043a void(*)(Object*, char*) \u0438 \u0432\u044b\u0437\u0432\u0430\u0442\u044c,  \u043f\u0435\u0440\u0435\u0434\u0430\u0432 \u0430\u0434\u0440\u0435\u0441 \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430.  *\/ void Object_Print_Repr(Object *object) {     void **p_repr_func = (void*) object + object->offset_repr;     void (*repr_func)(Object*, char*) = *p_repr_func;     char buf[80];     repr_func(object, buf);     printf(\"%s\\n\", buf); }  \/*  \u0422\u043e \u0436\u0435 \u0441\u0430\u043c\u043e\u0435, \u0442\u043e\u043b\u044c\u043a\u043e \u0442\u0435\u043f\u0435\u0440\u044c \u0432\u043c\u0435\u0441\u0442\u043e offset_repr \u0431\u0435\u0440\u0435\u043c offset_str.  \u0421\u0438\u0433\u043d\u0430\u0442\u0443\u0440\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0442\u0430\u043a\u0430\u044f \u0436\u0435, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0438\u0447\u0435\u0433\u043e \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u0433\u043e.  *\/ void Object_Print_Str(Object *object) {     void **p_str_func = (void*) object + object->offset_str;     void (*str_func)(Object*, char*) = *p_str_func;     char buf[80];     str_func(object, buf);     printf(\"%s\\n\", buf); }  \/*  \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u043f\u043e\u043b\u0435\u0439 \u0432 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0435,  \u0442\u0435\u043f\u0435\u0440\u044c \u0438\u0445 \u043c\u043e\u0436\u043d\u043e \u0433\u0440\u0443\u043f\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u0430\u043a \u0443\u0433\u043e\u0434\u043d\u043e.  *\/ struct Person {     \/* \"\u041d\u0430\u0441\u043b\u0435\u0434\u0443\u0435\u043c\u0441\u044f\" \u043e\u0442 Object *\/     Object object;      \/* \u0421\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0434\u0430\u043d\u043d\u044b\u0435 *\/     char *first_name;     char *last_name;      \/* \"\u041c\u0435\u0442\u043e\u0434\u044b\" *\/     void (*Repr)(Person*, char*);     void (*Str)(Person*, char*);     };  \/* Person->Repr(...) *\/ void Person_Repr(Person *person, char *buf) {     sprintf(buf, \"&lt;Person: first_name='%s' last_name='%s'>\",             person->first_name, person->last_name); }  \/* Person->Str(...) *\/ void Person_Str(Person *person, char *buf) {     sprintf(buf, \"%s %s\", person->first_name, person->last_name); }  \/*  \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f Person \u0438 \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u043e\u0439 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b Object  *\/ Person *New_Person(char *first_name, char *last_name) {     \/*      \u0421\u043e\u0431\u0438\u0440\u0430\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0435 \u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0441\u0430\u043c\u043e\u0433\u043e Person.      *\/     Person *person = malloc(sizeof(Person));     person->first_name = first_name;     person->last_name = last_name;     person->Repr = Person_Repr;     person->Str = Person_Str;      \/*      \u041e\u043f\u043e\u0432\u0435\u0449\u0430\u0435\u043c \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0439 Object \u043e\u0431 \u0430\u0434\u0440\u0435\u0441\u0430\u0445 \"\u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445\"      \u0444\u0443\u043d\u043a\u0446\u0438\u0439, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u044b \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u043c\u0441\u044f \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u0438\u0437 \u0441\u0430\u043c\u043e\u0433\u043e Object      *\/     person->object.offset_repr = offsetof(Person, Repr);     person->object.offset_str = offsetof(Person, Str);      \/* \u0418 \u043d\u0430\u043f\u043e\u043b\u043d\u044f\u0435\u043c \u0435\u0433\u043e \u0441\u043c\u044b\u0441\u043b\u043e\u043c *\/     person->object.Print_Repr = Object_Print_Repr;     person->object.Print_Str = Object_Print_Str;          return person; }  \/* \u041d\u0435 \u0437\u0430\u0431\u044b\u0432\u0430\u0435\u043c \u043f\u043e\u0434\u0447\u0438\u0449\u0430\u0442\u044c \u0437\u0430 \u0441\u043e\u0431\u043e\u0439 *\/ void Del_Person(Person *person) {     free(person); }  \/* \u0421\u043e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u043e\u0439 Bird \u0432\u0441\u0435 \u0440\u043e\u0432\u043d\u043e \u0442\u0430\u043a \u0436\u0435, \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0438 \u0438\u0437\u043b\u0438\u0448\u043d\u0438. *\/ struct Bird {     Object object;      char *name;     Person *owner;      void (*Repr)(Bird*, char*);     void (*Str)(Bird*, char*); };  void Bird_Repr(Bird *bird, char* buf) {     char owner_repr[80];     bird->owner->Repr(bird->owner, owner_repr);     sprintf(buf, \"&lt;Bird: name='%s' owner=%s>\",             bird->name, owner_repr); }  void Bird_Str(Bird *bird, char* buf) {     sprintf(buf, \"%s\", bird->name); }  Bird *New_Bird(char *name, Person *owner) {     Bird *bird = malloc(sizeof(Bird));     bird->name = name;     bird->owner = owner;     bird->Repr = Bird_Repr;     bird->Str = Bird_Str;      bird->object.offset_repr = offsetof(Bird, Repr);     bird->object.offset_str = offsetof(Bird, Str);      bird->object.Print_Repr = Object_Print_Repr;     bird->object.Print_Str = Object_Print_Str;      return bird; }  void Del_Bird(Bird *bird) {     free(bird); }  int main(void) {     Person *person = New_Person(\"Oleg\", \"Olegov\");     Bird *bird = New_Bird(\"Kukushka\", person);      \/*      \"\u0421\u043c\u043e\u0442\u0440\u0438\u043c\" \u043d\u0430 \u043e\u0431\u044a\u0435\u043a\u0442 person \u043a\u0430\u043a \u043d\u0430 Object      \u0438 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0441 \u044d\u0442\u0438\u043c \u0436\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u043c.      \u0412 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0435, \u043d\u0438\u043a\u0442\u043e \u043d\u0435 \u0437\u0430\u043f\u0440\u0435\u0449\u0430\u0435\u0442 \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c person      \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0431\u0435\u0437 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u0438\u044f \u0442\u0438\u043f\u0430:                ((Object*)person)->Print_Repr(person);       GCC \u044d\u0442\u043e \u0441\u0445\u0430\u0432\u0430\u0435\u0442, \u043d\u043e \u0432\u044b\u043a\u0438\u043d\u0435\u0442 warning.      *\/     ((Object*)person)->Print_Repr((Object*)person);     ((Object*)person)->Print_Str((Object*)person);          ((Object*)bird)->Print_Repr((Object*)bird);     ((Object*)bird)->Print_Str((Object*)bird);      Del_Bird(bird);     Del_Person(person); }<\/code><\/pre>\n<p>\u0412\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043f\u0440\u0438\u043a\u043e\u043b\u044c\u043d\u043e, \u043d\u043e \u0431\u0440\u0435\u0434\u043e\u0432\u0430\u0442\u043e. \u0412\u043e-\u043f\u0435\u0440\u0432\u044b\u0445, \u043c\u043d\u043e\u0433\u043e boilerplate-\u043a\u043e\u0434\u0430 \u0432 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0442\u043e\u0440\u0430\u0445, \u0430 \u0432\u043e-\u0432\u0442\u043e\u0440\u044b\u0445, \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u044b\u0439 \u043a\u0430\u0441\u0442\u0438\u043d\u0433 <code>(Object*)<\/code> \u043f\u0440\u043e\u0441\u0442\u043e \u043a\u0440\u0438\u0447\u0438\u0442 \u043e \u043f\u0440\u043e\u0442\u0435\u043a\u0430\u044e\u0449\u0438\u0445 \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0446\u0438\u044f\u0445.<\/p>\n<p>\u0412 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0435, \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u044e\u044e \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443 \u0440\u0435\u0448\u0438\u0442\u044c \u043d\u0435 \u0442\u0430\u043a \u0441\u043b\u043e\u0436\u043d\u043e. \u0414\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432\u0441\u0435 <code>Print_*<\/code> \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0432 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0438 \u0441\u043d\u0430\u0431\u0434\u0438\u0442\u044c \u0438\u0445 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438 \u043d\u0430 \u0442\u0435 \u0436\u0435 \u0441\u0430\u043c\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0438\u0437 <code>Object<\/code>:<\/p>\n<pre><code class=\"cpp\">struct Person {     \/* ... *\/     \/* \u0421\u0441\u044b\u043b\u043a\u0438 \u043d\u0430 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 Object *\/     void (*Print_Repr)(Person*);     void (*Print_Str)(Person*); };  Person *New_Person(char *first_name, char *last_name) {     \/* ... *\/     person->object.Print_Repr = Object_Print_Repr;     person->object.Print_Str = Object_Print_Str;      \/*      \u0412\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c \u0442\u0435 \u0436\u0435 \u0441\u0430\u043c\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0432 person,      \u043f\u0440\u0438\u0432\u0435\u0434\u044f \u0438\u0445 \u043a void (*)(Person *), \u0447\u0442\u043e\u0431\u044b \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440      \u043d\u0435 \u0440\u0443\u0433\u0430\u043b\u0441\u044f.      *\/     person->Print_Repr = (void (*)(Person *))Object_Print_Repr;     person->Print_Str = (void (*)(Person *))Object_Print_Str;          return person; }  \/* Bird - \u0442\u043e \u0436\u0435 \u0441\u0430\u043c\u043e\u0435 *\/  int main(void) {     \/* ... *\/     person->Print_Repr(person);     person->Print_Str(person);          bird->Print_Repr(bird);     bird->Print_Str(bird);      \/* ... *\/ }<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0441\u043e\u0432\u0441\u0435\u043c \u043a\u0440\u0430\u0441\u043e\u0442\u0430, \u041e\u041e\u041f \u0432\u043e \u0432\u0441\u0435 \u0449\u0435\u043b\u0438! \u0414\u0435\u0440\u0433\u0430\u0435\u043c \u043c\u0435\u0442\u043e\u0434 <code>person->Print_Repr()<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 <code>person->object.Print_Repr()<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435 \u0434\u0435\u0440\u0433\u0430\u0435\u0442 <code>person->Repr()<\/code>.<\/p>\n<p>\u041d\u043e boilerplate-\u043a\u043e\u0434\u0430 \u0432\u0441\u0435 \u0435\u0449\u0435 \u043d\u0435\u043f\u0440\u0438\u043b\u0438\u0447\u043d\u043e \u043c\u043d\u043e\u0433\u043e. \u041a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437 \u0432\u0441\u044e \u043d\u0430\u0448\u0443  \u041e\u041e\u041f-\u043c\u0430\u0448\u0438\u043d\u0435\u0440\u0438\u044e \u043d\u0443\u0436\u043d\u043e \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u0432 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0442\u043e\u0440\u0430\u0445, \u0438 \u043d\u0435 \u0434\u0430\u0439 \u0431\u043e\u0436\u0435 \u0447\u0442\u043e-\u0442\u043e  \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c &#8212; SEGFAULT \u043d\u0435 \u0434\u0440\u0435\u043c\u043b\u0435\u0442!<\/p>\n<p>\u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c &#8212; <code>object.h<\/code>:<\/p>\n<pre><code class=\"cpp\">#pragma once  #include &lt;stddef.h>  \/*  \u041c\u0430\u043a\u0440\u043e\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u0435\u0442 \u043d\u0443\u0436\u043d\u044b\u0435 \u043f\u043e\u043b\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u0430.  *\/ #define OBJECT(T) \\     Object object; \\     void (*Repr)(T*, char*); \\     void (*Str)(T*, char*); \\     void (*Print_Repr)(T*); \\     void (*Print_Str)(T*);  \/*  \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0442\u043e\u0440 \u043e\u0431\u044a\u0435\u043a\u0442\u0430, \u043f\u043e\u0434\u0441\u043e\u0432\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u0432\u0441\u0435  \u043d\u0443\u0436\u043d\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0438 \u043e\u0444\u0444\u0441\u0435\u0442\u044b  *\/ #define INIT_OBJECT(x, T) \\     x->object.Print_Repr = Object_Print_Repr; \\     x->object._offset_Repr = offsetof(T, Repr); \\     x->object.Print_Str = Object_Print_Str; \\     x->object._offset_Str = offsetof(T, Str); \\     x->Print_Repr = (void (*) (T*)) Object_Print_Repr; \\     x->Print_Str = (void (*) (T*)) Object_Print_Str; \\     x->Repr = T ## _Repr; \\     x->Str = T ## _Str  \/* \u041c\u0430\u043a\u0440\u043e\u0441, \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044e\u0449\u0438\u0439 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043f\u043e \u0435\u0435 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044e *\/ #define OBJECT_FUNC(x, F) *(void **)((void*) x + x->_offset_ ## F)  typedef struct Object Object;  typedef void *(Repr)(Object *, char*); typedef void *(Str)(Object *, char*);  \/* \u041d\u0430\u0448\u0438 \u0441\u0442\u0430\u0440\u044b\u0435 \u0437\u043d\u0430\u043a\u043e\u043c\u044b\u0435 *\/ struct Object {     size_t _offset_Repr;     void (*Print_Repr)(Object*);      size_t _offset_Str;     void (*Print_Str)(Object*); };  void Object_Print_Repr(Object *object) {     Repr *repr_func = OBJECT_FUNC(object, Repr);     char buf[80];     repr_func(object, buf);     printf(\"%s\\n\", buf); }  void Object_Print_Str(Object *object) {     Str *str_func = OBJECT_FUNC(object, Str);     char buf[80];     str_func(object, buf);     printf(\"%s\\n\", buf); }<\/code><\/pre>\n<p>\u0418 \u0432\u043e\u0442 \u043a\u0430\u043a \u044d\u0442\u0438 \u043c\u0430\u043a\u0440\u043e\u0441\u044b \u0441\u043e\u043a\u0440\u0430\u0449\u0430\u044e\u0442 \u043e\u0431\u044a\u0435\u043c \u0444\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430:<\/p>\n<pre><code class=\"cpp\">typedef struct Person Person; typedef struct Bird Bird;  struct Person {     \/*      \u042d\u0442\u043e \u043d\u0435 \u043e\u0431\u044b\u0447\u043d\u0430\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430, \u0430 \u043d\u0430\u0441\u043b\u0435\u0434\u043d\u0438\u043a      \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0446\u0438\u0438 \u043f\u043e \u0438\u043c\u0435\u043d\u0438 Object      *\/     OBJECT(Person)     char *first_name;     char *last_name; };  void Person_Repr(Person *person, char *buf) {     sprintf(buf, \"&lt;Person: first_name='%s' last_name='%s'>\",             person->first_name, person->last_name); }  void Person_Str(Person *person, char *buf) {     sprintf(buf, \"%s %s\", person->first_name, person->last_name); }  Person *New_Person(char *first_name, char *last_name) {     Person *person = malloc(sizeof(Person));      \/*      INIT_OBJECT() \u0446\u0435\u043f\u043b\u044f\u0435\u0442 \u0432\u0441\u0435 \u043d\u0443\u0436\u043d\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438,      \u0432\u043a\u043b\u044e\u0447\u0430\u044f Person_Repr \u0438 Person_Str, \u0438 \u043f\u043e\u0434\u0441\u043e\u0432\u044b\u0432\u0430\u0435\u0442      \u0438\u0445 \u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435 \u043f\u043e\u043b\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b      *\/     INIT_OBJECT(person, Person);      person->first_name = first_name;     person->last_name = last_name;      return person; }  \/*  \u0418\u0437\u0432\u0438\u043d\u0438\u0442\u0435, \u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f garbage collector \u043d\u0430 \u0421\u0438 -  \u0442\u0435\u043c\u0430 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u0432\u044b\u043f\u0443\u0441\u043a\u0430  *\/ void Del_Person(Person *person) {     free(person); }  \/* Bird \u0441\u043d\u043e\u0432\u0430 \u043d\u0438\u0447\u0435\u043c \u043d\u0435 \u043e\u0442\u043b\u0438\u0447\u0430\u0435\u0442\u0441\u044f \u043e\u0442 Person *\/ struct Bird {     OBJECT(Bird)     char *name;     Person *owner; };  void Bird_Repr(Bird *bird, char* buf) {     char owner_repr[80];     bird->owner->Repr(bird->owner, owner_repr);     sprintf(buf, \"&lt;Bird: name='%s' owner=%s>\",             bird->name, owner_repr); }  void Bird_Str(Bird *bird, char* buf) {     sprintf(buf, \"%s\", bird->name); }  Bird *New_Bird(char *name, Person *owner) {     Bird *bird = malloc(sizeof(Bird));     INIT_OBJECT(bird, Bird);      bird->name = name;     bird->owner = owner;     return bird; }  void Del_Bird(Bird *bird) {     free(bird); }  int main(void) {     Person *person = New_Person(\"Oleg\", \"Olegov\");     Bird *bird = New_Bird(\"Kukushka\", person);      \/*      \u0412\u044b\u0437\u044b\u0432\u0430\u0435\u043c \u0440\u0430\u0437\u043d\u044b\u0435 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u044b \"\u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u0445\" \u0444\u0443\u043d\u043a\u0446\u0438\u0439      Print_Repr \u0438 Print_Str      *\/     person->Print_Repr(person);     bird->Print_Repr(bird);      person->Print_Str(person);     bird->Print_Str(bird);      Del_Bird(bird);     Del_Person(person); }<\/code><\/pre>\n<p>\u0421\u0430\u043c\u043e\u0435 \u043f\u0440\u0435\u043b\u0435\u0441\u0442\u043d\u043e\u0435 \u0432 \u044d\u0442\u0438\u0445 \u043c\u0430\u043a\u0440\u043e\u0441\u0430\u0445 &#8212; \u044d\u0442\u043e  \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0435\u043d\u0438\u0435 compile-time \u043f\u0440\u043e\u0432\u0435\u0440\u043e\u043a. \u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c, \u043c\u044b \u0440\u0435\u0448\u0438\u043b\u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043d\u043e\u0432\u0443\u044e  \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443, &#171;\u043d\u0430\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043b\u0438&#187; \u0435\u0435 \u043e\u0442 <code>Object<\/code>, \u043d\u043e \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 <code>Repr<\/code> \u0438 <code>Str<\/code> \u043d\u0435 \u043e\u0431\u044a\u044f\u0432\u0438\u043b\u0438:<\/p>\n<pre><code class=\"cpp\">typedef struct Fruit Fruit;  struct Fruit {     OBJECT(Fruit)     char *name; };  Fruit *New_Fruit(char *name) {     Fruit *fruit = malloc(sizeof(Fruit));     INIT_OBJECT(fruit, Fruit);      fruit->name = name;     return fruit; }  void Del_Fruit(Fruit *fruit) {     free(fruit); }<\/code><\/pre>\n<p>\u0418 \u0442\u043e\u0433\u0434\u0430 \u043d\u0430\u043c \u043d\u0435\u0437\u0430\u043c\u0435\u0434\u043b\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u0440\u0438\u043b\u0435\u0442\u0430\u0435\u0442 \u043e\u0442 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0430:<\/p>\n<pre><code>c_inheritance.c: In function \u2018New_Fruit\u2019: c_inheritance.c:77:24: error: \u2018Fruit_Repr\u2019 undeclared (first use in this function)    77 |     INIT_OBJECT(fruit, Fruit);       |                        ^~~~~ &lt;...> c_inheritance.c:77:24: error: \u2018Fruit_Str\u2019 undeclared (first use in this function)    77 |     INIT_OBJECT(fruit, Fruit);       |                        ^~~~~<\/code><\/pre>\n<p>\u041e\u0447\u0435\u043d\u044c \u0443\u0434\u043e\u0431\u043d\u043e!<\/p>\n<p>\u0410 \u0432 \u0447\u0435\u043c \u0436\u0435 \u0437\u0434\u0435\u0441\u044c \u0441\u0442\u0440\u0435\u043b\u044f\u043d\u0438\u0435 \u043f\u043e \u043d\u043e\u0433\u0430\u043c? &#8212; \u0441\u043f\u0440\u043e\u0441\u0438\u0442\u0435 \u0432\u044b. \u0420\u0430\u0437 \u0443\u0436 \u0432\u0441\u0435 \u0442\u0430\u043a \u043a\u043b\u0435\u0432\u043e, \u043f\u043e\u0447\u0435\u043c\u0443 \u0431\u044b \u043d\u0435 \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u044c \u044d\u0442\u043e \u0432 \u043f\u0440\u043e\u043c\u044b\u0448\u043b\u0435\u043d\u043d\u043e\u0439 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0435?<\/p>\n<p>\u0412\u043e-\u043f\u0435\u0440\u0432\u044b\u0445, \u0432 \u043a\u043e\u043c\u0430\u043d\u0434\u0435 \u0432\u0430\u0441 \u0431\u0443\u0434\u0443\u0442 \u0441\u0447\u0438\u0442\u0430\u0442\u044c \u043d\u0430\u0440\u043a\u043e\u043c\u0430\u043d\u043e\u043c.<\/p>\n<p>\u0412\u043e-\u0432\u0442\u043e\u0440\u044b\u0445, \u0434\u0430\u0436\u0435 \u0435\u0441\u043b\u0438 \u043d\u0435 \u0431\u0443\u0434\u0443\u0442, \u0442\u043e \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u044c \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b \u0441\u043d\u0438\u0437\u0438\u0442\u0441\u044f. \u0410 \u0421\u0438  \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 \u043a\u0430\u043a \u0440\u0430\u0437 \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u044d\u0442\u0443 \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u044c \u043f\u0440\u0438\u043e\u0431\u0440\u0435\u0441\u0442\u0438, \u0438 \u0447\u0430\u0441\u0442\u043e \u0437\u0430  \u043d\u0435\u0435 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043f\u043b\u0430\u0442\u0438\u0442\u044c \u0434\u0443\u0431\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u043a\u043e\u0434\u0430 \u0438 \u0438\u0437\u0431\u0435\u0433\u0430\u043d\u0438\u0435\u043c \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0446\u0438\u0439. \u0418  \u043d\u0435\u0441\u043c\u043e\u0442\u0440\u044f \u043d\u0430 \u0442\u043e, \u0447\u0442\u043e \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u044b \u043d\u044b\u043d\u0447\u0435 \u0441\u0443\u043f\u0435\u0440-\u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u0443\u044e\u0449\u0438\u0435, \u0430\u0441\u0441\u0435\u043c\u0431\u043b\u0435\u0440\u043d\u044b\u0439  \u0432\u044b\u0445\u043b\u043e\u043f \u0438\u0437 &#171;\u041e\u041e\u041f&#187;-\u043a\u043e\u0434\u0430 \u0438 \u043a\u043e\u0434\u0430 \u0441 \u043f\u0430\u0440\u043e\u0439 \u043f\u0440\u043e\u0441\u0442\u044b\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 <code>Person_Print()<\/code> \u0438 <code>Bird_Print()<\/code> \u0434\u0430\u0436\u0435 \u0441 <code>-O3<\/code> \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0437\u043b\u0438\u0447\u0430\u0442\u044c\u0441\u044f \u0432 \u043f\u043e\u043b\u0442\u043e\u0440\u0430-\u0434\u0432\u0430 \u0440\u0430\u0437\u0430 (\u043d\u0435 \u0432 \u043f\u043e\u043b\u044c\u0437\u0443 \u043f\u0435\u0440\u0432\u043e\u0433\u043e).<\/p>\n<p>\u041f\u043e\u0441\u0435\u043c\u0443 \u0434\u0430\u043d\u043d\u0430\u044f \u0441\u0442\u0430\u0442\u044c\u044f \u043d\u043e\u0441\u0438\u0442 \u0438\u0441\u043a\u043b\u044e\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u043e\u043d\u043d\u044b\u0439 \u0445\u0430\u0440\u0430\u043a\u0442\u0435\u0440, \u0430 \u043d\u0438\u043a\u0430\u043a \u043d\u0435 \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"v-portal\" style=\"display:none;\"><\/div>\n<\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/673270\/\"> https:\/\/habr.com\/ru\/post\/673270\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<blockquote>\n<p><sup>TL;DR<\/sup> <a href=\"https:\/\/github.com\/pomidoroshev\/c-inheritance\" rel=\"noopener noreferrer nofollow\">https:\/\/github.com\/pomidoroshev\/c-inheritance<\/a><\/p>\n<\/blockquote>\n<p>\u0418\u043d\u043e\u0433\u0434\u0430 \u043d\u0435\u0442-\u043d\u0435\u0442 \u0434\u0430 \u0438 \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u0447\u0442\u043e-\u043d\u0438\u0431\u0443\u0434\u044c \u0430\u0431\u0441\u0442\u0440\u0430\u0433\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438 \u043e\u0431\u043e\u0431\u0449\u0438\u0442\u044c \u0432  \u043a\u043e\u0434\u0435 \u043d\u0430 \u0421\u0438. \u041a \u043f\u0440\u0438\u043c\u0435\u0440\u0443, \u0445\u043e\u0447\u0435\u0448\u044c \u0442\u044b \u043f\u0440\u0438\u043d\u0442\u0430\u043d\u0443\u0442\u044c \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b  \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0440\u0430\u0437, \u043f\u0438\u0448\u0435\u0448\u044c \u0432\u0435\u0437\u0434\u0435, \u043a\u0430\u043a \u0434\u0443\u0440\u0430\u043a, <code>printf(\"%s %d %f\\n\", foo->bar, foo->baz, foo->boom)<\/code>, \u0438 \u0438\u043d\u0442\u0443\u0438\u0442\u0438\u0432\u043d\u043e \u043a\u0430\u0436\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u0435\u0441\u0442\u044c \u0441\u043f\u043e\u0441\u043e\u0431 \u0441\u0434\u0435\u043b\u0430\u0442\u044c <code>foo->print(foo)<\/code>, \u0438 \u0442\u0430\u043a \u0432\u043e\u043e\u0431\u0449\u0435 \u0441\u043e \u0432\u0441\u0435\u043c\u0438 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430\u043c\u0438, \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0441 <code>foo<\/code>.<\/p>\n<p>\u0412\u043e\u0437\u044c\u043c\u0435\u043c \u043f\u0440\u0438\u043c\u0435\u0440: \u0435\u0441\u0442\u044c \u043d\u0435\u043a\u0438\u0439 \u0447\u0443\u0432\u0430\u043a \u0441 \u0438\u043c\u0435\u043d\u0435\u043c \u0438 \u0444\u0430\u043c\u0438\u043b\u0438\u0435\u0439, \u0438 \u0435\u0441\u0442\u044c \u043f\u0442\u0438\u0446\u0430, \u0443 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0435\u0441\u0442\u044c \u0438\u043c\u044f \u0438 \u0432\u043b\u0430\u0434\u0435\u043b\u0435\u0446.<\/p>\n<pre><code class=\"cpp\">typedef struct Person Person; struct Person {     char *first_name;     char *last_name; };  typedef struct Bird Bird; struct Bird {     char *name;     Person *owner; };<\/code><\/pre>\n<p>\u0427\u0442\u043e\u0431\u044b \u0432\u044b\u0432\u0435\u0441\u0442\u0438 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043f\u0440\u043e \u044d\u0442\u0438\u0445 \u0436\u0438\u0432\u043e\u0442\u043d\u044b\u0445, \u043a\u043e\u043d\u0434\u043e\u0432\u044b\u0439 \u0441\u0438\u0448\u043d\u0438\u043a \u043d\u0430\u043f\u0438\u0448\u0435\u0442 \u043f\u0440\u043e\u0441\u0442\u043e \u0434\u0432\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438:<\/p>\n<pre><code class=\"cpp\">void Person_Print(Person *p) {     printf(\"%s %s\\n\", p->first_name, p->last_name); }  void Bird_Print(Bird *b) {     printf(\"%s of %s %s\\n\", b->name, b->owner->first_name, b->owner->last_name); }<\/code><\/pre>\n<p>\u0418 \u0431\u0443\u0434\u0435\u0442 \u0442\u0430\u043a\u0438 \u043f\u0440\u0430\u0432! \u041d\u043e \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0445  \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440 \u0443 \u043d\u0430\u0441 \u043c\u043d\u043e\u0433\u043e, \u0430 \u043d\u0430\u0448 \u043c\u043e\u0437\u0433 \u0438\u0441\u043f\u043e\u0440\u0447\u0435\u043d \u0432\u0435\u044f\u043d\u0438\u044f\u043c\u0438 \u041e\u041e\u041f? \u041f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e, \u043d\u0430\u0434\u043e \u0443  \u043a\u0430\u0436\u0434\u043e\u0439 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u043e\u0431\u0449\u0438\u0439 \u043c\u0435\u0442\u043e\u0434, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 <code>void Repr(Person* person, char* buf)<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u0431\u0440\u043e\u0441\u0438\u0442 \u0432 <code>buf<\/code>  \u0441\u0442\u0440\u043e\u043a\u043e\u0432\u043e\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 (\u0434\u0430, \u0442\u0435\u043f\u0435\u0440\u044c \u0443 \u043d\u0430\u0441 \u043f\u043e\u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u044b), \u0438  \u0434\u0430\u043b\u044c\u0448\u0435 \u043c\u044b \u0431\u044b \u043c\u043e\u0433\u043b\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u043e\u0442 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0434\u043b\u044f \u0432\u044b\u0432\u043e\u0434\u0430 \u043d\u0430 \u044d\u043a\u0440\u0430\u043d:<\/p>\n<pre><code class=\"cpp\">\/* Person *\/ struct Person {     void (*Repr)(Person*, char*);     \/* ... *\/ };  void Person_Repr(Person *person, char *buf) {     sprintf(buf, \"&lt;Person: first_name='%s' last_name='%s'>\",             person->first_name, person->last_name); }  Person *New_Person(char *first_name, char *last_name) {     Person *person = malloc(sizeof(Person));     person->Repr = Person_Repr;     person->first_name = first_name;     person->last_name = last_name;     return person; }  \/* Bird *\/ struct Bird {     void (*Repr)(Bird*, char*);     \/* ... *\/ };  void Bird_Repr(Bird *bird, char* buf) {     char owner_repr[80];     bird->owner->Repr(bird->owner, owner_repr);     sprintf(buf, \"&lt;Bird: name='%s' owner=%s>\",             bird->name, owner_repr); }  Bird *New_Bird(char *name, Person *owner) {     Bird *bird = malloc(sizeof(Bird));     bird->Repr = Bird_Repr;     bird->name = name;     bird->owner = owner;     return bird; }<\/code><\/pre>\n<p>\u041e\u043a\u0435\u0439, \u0432\u0440\u043e\u0434\u0435 \u0443\u043d\u0438\u0444\u0438\u0446\u0438\u0440\u043e\u0432\u0430\u043b\u0438, \u0434\u0430 \u043d\u0435 \u043e\u0447\u0435\u043d\u044c.  \u041a\u0430\u043a \u0442\u0435\u043f\u0435\u0440\u044c \u044d\u0442\u0438 \u043c\u0435\u0442\u043e\u0434\u044b \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c? \u041d\u0435 \u043e\u0447\u0435\u043d\u044c \u0443\u0434\u043e\u0431\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f, \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437  \u0432\u044b\u043b\u0435\u0437\u0430\u0435\u0442 \u0441\u0432\u0438\u0441\u0442\u043e\u043f\u043b\u044f\u0441\u043a\u0430 \u0441 \u0431\u0443\u0444\u0435\u0440\u0430\u043c\u0438:<\/p>\n<pre><code class=\"cpp\">char buf[80]; bird->Repr(bird, buf); printf(\"%s\\n\", buf);<\/code><\/pre>\n<p>\u041a\u0430\u043a \u0432\u0430\u0440\u0438\u0430\u043d\u0442 &#8212; \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0431\u0430\u0437\u043e\u0432\u0443\u044e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 <code>Object<\/code>, \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0432 \u043d\u0435\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u044e <code>Print()<\/code>, &#171;\u043d\u0430\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u044c&#187; \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u043e\u0442 <code>Object<\/code> \u0438 \u0432 <code>Object::Print()<\/code> \u0434\u0435\u0440\u0433\u0430\u0442\u044c \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u043c\u0435\u0442\u043e\u0434 <code>Repr()<\/code>. \u0412\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043b\u043e\u0433\u0438\u0447\u043d\u043e, \u0442\u043e\u043b\u044c\u043a\u043e \u043c\u044b \u043f\u0438\u0448\u0435\u043c \u043d\u0430 \u0421\u0438, \u0430 \u043d\u0435 \u043d\u0430 \u043f\u043b\u044e\u0441\u0430\u0445, \u0433\u0434\u0435 \u0442\u0430\u043a\u043e\u0435 \u043d\u0430 \u0440\u0430\u0437-\u0434\u0432\u0430 \u0440\u0435\u0448\u0430\u0435\u0442\u0441\u044f \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u043c\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u043c\u0438.<\/p>\n<p>\u041d\u043e \u0432 \u0421\u0438 \u0435\u0441\u0442\u044c \u0442\u0430\u043a\u0430\u044f \u0448\u0442\u0443\u043a\u0430: \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0438\u0432\u0435\u0441\u0442\u0438 \u043e\u0434\u043d\u0443 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u043a \u0434\u0440\u0443\u0433\u043e\u0439, \u0435\u0441\u043b\u0438 \u0443 \u043d\u0435\u0435 \u0442\u0430 \u0434\u0440\u0443\u0433\u0430\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0438\u0434\u0435\u0442 \u043f\u0435\u0440\u0432\u044b\u043c \u043f\u043e\u043b\u0435\u043c.<\/p>\n<p>\u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"cpp\">typedef struct {     int i; } Foo;  typedef struct {     Foo foo;     int j; } Bar;  Bar *bar = malloc(sizeof(Bar)); bar->foo.i = 123; printf(\"%d\\n\", ((Foo*)bar)->i);<\/code><\/pre>\n<p>\u0422\u043e \u0435\u0441\u0442\u044c \u043c\u044b \u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 <code>bar<\/code>, \u043d\u043e \u0441 \u0442\u0438\u043f\u043e\u043c <code>Foo<\/code>, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043f\u043e \u0441\u0443\u0442\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 &#8212; \u044d\u0442\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u0435\u0435 \u043f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442, \u0438 \u0442\u0443\u0442 \u043c\u044b \u0438\u043c\u0435\u0435\u043c \u043f\u0440\u0430\u0432\u043e \u0442\u0430\u043a \u043a\u0430\u0441\u0442\u043e\u0432\u0430\u0442\u044c.<\/p>\n<p>\u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0431\u0430\u0437\u043e\u0432\u0443\u044e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 <code>Object<\/code> \u0441 \u043e\u0434\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0435\u0439 <code>Print_Repr()<\/code>, \u043a\u043e\u0442\u043e\u0440\u0430\u044f, \u043f\u043e \u0438\u0434\u0435\u0435, \u0434\u043e\u043b\u0436\u043d\u0430 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0437\u0432\u0430\u0442\u044c &#171;\u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u043c\u0435\u0442\u043e\u0434&#187; <code>Repr()<\/code> \u0443 \u043d\u0430\u0448\u0438\u0445 \u043b\u044e\u0434\u0438\u0448\u0435\u043a \u0438 \u043f\u0442\u0438\u0447\u0435\u043a:<\/p>\n<pre><code class=\"cpp\">typedef struct Object Object; struct Object {     void (*Print_Repr)(Object*); };  \/*  \u0421\u0430\u043c\u0430\u044f \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u0430\u044f \u0447\u0430\u0441\u0442\u044c. \u0424\u0443\u043d\u043a\u0446\u0438\u044f \u0431\u0435\u0440\u0435\u0442 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c  \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435 \u043f\u043e\u043b\u0435 \u0432 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0435 \u043f\u043e\u0441\u043b\u0435 Object,  \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0432 \u0442\u0435\u043a\u0443\u0449\u0435\u043c \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0435\u043c  \u043d\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044e Repr().  *\/ void Object_Print_Repr(Object *object) {     void **p_repr_func = (void*) object + sizeof(Object);     void (*repr_func)(Object*, char*) = *p_repr_func;     char buf[80];     repr_func(object, buf);     printf(\"%s\\n\", buf); }  \/* Person *\/ typedef struct Person Person; struct Person {     Object object;     void (*Repr)(Person*, char*);     \/* ... *\/ };  Person *New_Person(char *first_name, char *last_name) {     Person *person = malloc(sizeof(Person));     person->object.Print_Repr = Object_Print_Repr;     person->Repr = Person_Repr;     \/* ... *\/     return person; }  \/* Bird *\/ typedef struct Bird Bird; struct Bird {     Object object;     void (*Repr)(Bird*, char*);     \/* ... *\/ };  Bird *New_Bird(char *name, Person *owner) {     Bird *bird = malloc(sizeof(Bird));     bird->object.Print_Repr = Object_Print_Repr;     bird->Repr = Bird_Repr;     \/* ... *\/     return bird; }<\/code><\/pre>\n<p>\u0412\u043e\u0442 \u043c\u044b \u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043b\u0438 \u043f\u0430\u0442\u0442\u0435\u0440\u043d &#171;\u0428\u0430\u0431\u043b\u043e\u043d\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434&#187; \u043d\u0430 \u0447\u0438\u0441\u0442\u043e\u043c \u0421\u0438. \u041d\u0435 \u0441\u043e\u0432\u0441\u0435\u043c \u0447\u0435\u0441\u0442\u043d\u043e, \u0438 \u043d\u0435 \u0441\u043e\u0432\u0441\u0435\u043c \u043d\u0430\u0434\u0435\u0436\u043d\u043e, \u043d\u043e \u043a\u043e\u0435-\u043a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442.<\/p>\n<p>\u0422\u0443\u0442 \u0434\u0432\u0430 \u0432\u043e\u043f\u0440\u043e\u0441\u0430:<\/p>\n<ol>\n<li>\n<p>\u041a\u0430\u043a \u0431\u044b\u0442\u044c, \u0435\u0441\u043b\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u044f <code>Repr()<\/code> \u043d\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0432\u0442\u043e\u0440\u044b\u043c \u043f\u043e\u043b\u0435\u043c \u0432 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0435?<\/p>\n<\/li>\n<li>\n<p>\u041a\u0430\u043a \u0431\u044b\u0442\u044c, \u0435\u0441\u043b\u0438 \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0438 \u0431\u043e\u043b\u0435\u0435 \u0447\u0435\u043c \u043e\u0434\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438?<\/p>\n<\/li>\n<\/ol>\n<p>\u041e\u0442\u0432\u0435\u0442 \u043d\u0435 \u0441\u0430\u043c\u044b\u0439 \u043f\u0440\u0438\u044f\u0442\u043d\u044b\u0439, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043f\u043e\u0440\u0442\u0438\u0442 \u0432\u0441\u044e \u043a\u0440\u0430\u0441\u043e\u0442\u0443 \u0438 \u0447\u0438\u0441\u0442\u043e\u0442\u0443 \u0431\u0430\u0437\u043e\u0432\u043e\u0439 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b <code>Object<\/code>, \u0442\u0443\u0434\u0430 \u043d\u0430\u0434\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0430\u0434\u0440\u0435\u0441\u0430 \u043d\u0443\u0436\u043d\u044b\u0445 \u043d\u0430\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u0439. \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u0445 \u043d\u0435\u0441\u043b\u043e\u0436\u043d\u043e, \u0432 <code>stddef.h<\/code> \u0435\u0441\u0442\u044c \u043f\u043e\u043b\u0435\u0437\u043d\u044b\u0439 \u043c\u0430\u043a\u0440\u043e\u0441 <code>offsetof(&lt;struct>, &lt;field>)<\/code>. \u0420\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043e\u043d \u0442\u0430\u043a:<\/p>\n<pre><code class=\"cpp\">struct A {     char c;     int i;     long l; }  offsetof(struct A, c) == 0; offsetof(struct A, i) == 4; offsetof(struct A, l) == 8;<\/code><\/pre>\n<p>\u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u044d\u0442\u043e\u0433\u043e \u043c\u0430\u043a\u0440\u043e\u0441\u0430 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043e\u0444\u0444\u0441\u0435\u0442\u044b \u0432\u0441\u0435\u0445 \u043d\u0443\u0436\u043d\u044b\u0445 generic-\u0444\u0443\u043d\u043a\u0446\u0438\u0439, \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0438\u0445 \u0432 <code>Object<\/code>, \u0438 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u0438\u0445 \u043e\u0442\u0442\u0443\u0434\u0430 \u0438\u0437 \u0434\u0440\u0443\u0433\u0438\u0445 \u043c\u0435\u0442\u043e\u0434\u043e\u0432. \u041a\u0440\u0430\u0441\u0438\u0432\u043e? \u0410 \u0442\u043e!<\/p>\n<p>\u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c, \u043a \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>Repr()<\/code> \u043c\u044b \u0437\u0430\u0445\u043e\u0442\u0435\u043b\u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e <code>Str()<\/code>, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u0442 \u043e\u0431\u044a\u0435\u043a\u0442 \u0432 \u0432\u0438\u0434\u0435 \u0441\u0442\u0440\u043e\u043a\u0438, \u043d\u043e \u0431\u0435\u0437 \u0432\u0441\u044f\u043a\u043e\u0439 \u0434\u0435\u0431\u0430\u0436\u043d\u043e\u0439 \u0448\u0435\u043b\u0443\u0445\u0438, \u0442\u0438\u043f\u0430 <code>&lt;Person first_name='Ivan' last_name='Ivanov'><\/code>, \u0430 \u043f\u0440\u043e\u0441\u0442\u043e \u0441\u0444\u043e\u0440\u043c\u0438\u0440\u0443\u0435\u0442 \u0441\u0442\u0440\u043e\u043a\u0443 <code>Ivan Ivanov<\/code> \u0434\u043b\u044f \u0432\u044b\u0432\u043e\u0434\u0430 \u0432 \u043a\u0430\u043a\u043e\u043c-\u0442\u043e \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0435. (\u0427\u0443\u0432\u0441\u0442\u0432\u0443\u0435\u0442\u0435 \u0432\u0435\u044f\u043d\u0438\u0435 Python \u0441 \u0435\u0433\u043e <code>__repr__()<\/code> \u0438 <code>__str__()<\/code>? \u041e\u043d\u043e \u0437\u0434\u0435\u0441\u044c \u043d\u0435 \u043f\u0440\u043e\u0441\u0442\u043e \u0442\u0430\u043a, <s>\u0430 \u0441\u043b\u043e\u0436\u043d\u043e \u0442\u0430\u043a<\/s>.)<\/p>\n<p>\u0421\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e, <code>Object<\/code> \u0434\u043e\u043b\u0436\u0435\u043d \u0438\u043c\u0435\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e <code>Print_Str()<\/code> \u0434\u043b\u044f \u0432\u044b\u0432\u043e\u0434\u0430 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432. \u0410 \u0447\u0442\u043e\u0431\u044b \u043e\u043d \u0446\u0435\u043f\u043b\u044f\u043b \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u043d\u0443\u0436\u043d\u043e \u0432\u043d\u0443\u0442\u0440\u0438 \u043d\u0435\u0433\u043e \u043f\u0440\u0438\u043a\u043e\u043f\u0430\u0442\u044c \u0432\u0441\u0435 \u043e\u0444\u0444\u0441\u0435\u0442\u044b.<\/p>\n<p>\u041b\u0438\u0441\u0442\u0438\u043d\u0433 \u0431\u0443\u0434\u0435\u0442 \u0431\u043e\u043b\u044c\u0448\u0435 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0445, \u0441 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u043c\u0438, \u043d\u043e \u0432\u044b \u043d\u0435 \u0431\u043e\u0439\u0442\u0435\u0441\u044c, \u043c\u044b \u0432\u0441\u0435 \u044d\u0442\u043e \u0441\u043a\u043e\u0440\u043e \u043f\u043e\u0440\u0435\u0444\u0430\u043a\u0442\u043e\u0440\u0438\u043c.<\/p>\n<pre><code class=\"cpp\">#include &lt;stdio.h> #include &lt;stdlib.h> #include &lt;stddef.h>  typedef struct Object Object; typedef struct Person Person; typedef struct Bird Bird;  struct Object {     size_t offset_repr;     void (*Print_Repr)(Object*);      size_t offset_str;     void (*Print_Str)(Object*); };  \/*  \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043f\u043e \u0430\u0434\u0440\u0435\u0441\u0443 object + offset_repr,  \u043a\u0430\u0441\u0442\u0430\u043d\u0443\u0442\u044c \u0435\u0435 \u043a void(*)(Object*, char*) \u0438 \u0432\u044b\u0437\u0432\u0430\u0442\u044c,  \u043f\u0435\u0440\u0435\u0434\u0430\u0432 \u0430\u0434\u0440\u0435\u0441 \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430.  *\/ void Object_Print_Repr(Object *object) {     void **p_repr_func = (void*) object + object->offset_repr;     void (*repr_func)(Object*, char*) = *p_repr_func;     char buf[80];     repr_func(object, buf);     printf(\"%s\\n\", buf); }  \/*  \u0422\u043e \u0436\u0435 \u0441\u0430\u043c\u043e\u0435, \u0442\u043e\u043b\u044c\u043a\u043e \u0442\u0435\u043f\u0435\u0440\u044c \u0432\u043c\u0435\u0441\u0442\u043e offset_repr \u0431\u0435\u0440\u0435\u043c offset_str.  \u0421\u0438\u0433\u043d\u0430\u0442\u0443\u0440\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0442\u0430\u043a\u0430\u044f \u0436\u0435, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0438\u0447\u0435\u0433\u043e \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u0433\u043e.  *\/ void Object_Print_Str(Object *object) {     void **p_str_func = (void*) object + object->offset_str;     void (*str_func)(Object*, char*) = *p_str_func;     char buf[80];     str_func(object, buf);     printf(\"%s\\n\", buf); }  \/*  \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u043f\u043e\u043b\u0435\u0439 \u0432 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0435,  \u0442\u0435\u043f\u0435\u0440\u044c \u0438\u0445 \u043c\u043e\u0436\u043d\u043e \u0433\u0440\u0443\u043f\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u0430\u043a \u0443\u0433\u043e\u0434\u043d\u043e.  *\/ struct Person {     \/* \"\u041d\u0430\u0441\u043b\u0435\u0434\u0443\u0435\u043c\u0441\u044f\" \u043e\u0442 Object *\/     Object object;      \/* \u0421\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0434\u0430\u043d\u043d\u044b\u0435 *\/     char *first_name;     char *last_name;      \/* \"\u041c\u0435\u0442\u043e\u0434\u044b\" *\/     void (*Repr)(Person*, char*);     void (*Str)(Person*, char*);     };  \/* Person->Repr(...) *\/ void Person_Repr(Person *person, char *buf) {     sprintf(buf, \"&lt;Person: first_name='%s' last_name='%s'>\",             person->first_name, person->last_name); }  \/* Person->Str(...) *\/ void Person_Str(Person *person, char *buf) {     sprintf(buf, \"%s %s\", person->first_name, person->last_name); }  \/*  \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f Person \u0438 \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u043e\u0439 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b Object  *\/ Person *New_Person(char *first_name, char *last_name) {     \/*      \u0421\u043e\u0431\u0438\u0440\u0430\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0435 \u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0441\u0430\u043c\u043e\u0433\u043e Person.      *\/     Person *person = malloc(sizeof(Person));     person->first_name = first_name;     person->last_name = last_name;     person->Repr = Person_Repr;     person->Str = Person_Str;      \/*      \u041e\u043f\u043e\u0432\u0435\u0449\u0430\u0435\u043c \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0439 Object \u043e\u0431 \u0430\u0434\u0440\u0435\u0441\u0430\u0445 \"\u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445\"      \u0444\u0443\u043d\u043a\u0446\u0438\u0439, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u044b \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u043c\u0441\u044f \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u0438\u0437 \u0441\u0430\u043c\u043e\u0433\u043e Object      *\/     person->object.offset_repr = offsetof(Person, Repr);     person->object.offset_str = offsetof(Person, Str);      \/* \u0418 \u043d\u0430\u043f\u043e\u043b\u043d\u044f\u0435\u043c \u0435\u0433\u043e \u0441\u043c\u044b\u0441\u043b\u043e\u043c *\/     person->object.Print_Repr = Object_Print_Repr;     person->object.Print_Str = Object_Print_Str;          return person; }  \/* \u041d\u0435 \u0437\u0430\u0431\u044b\u0432\u0430\u0435\u043c \u043f\u043e\u0434\u0447\u0438\u0449\u0430\u0442\u044c \u0437\u0430 \u0441\u043e\u0431\u043e\u0439 *\/ void Del_Person(Person *person) {     free(person); }  \/* \u0421\u043e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u043e\u0439 Bird \u0432\u0441\u0435 \u0440\u043e\u0432\u043d\u043e \u0442\u0430\u043a \u0436\u0435, \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0438 \u0438\u0437\u043b\u0438\u0448\u043d\u0438. *\/ struct Bird {     Object object;      char *name;     Person *owner;      void (*Repr)(Bird*, char*);     void (*Str)(Bird*, char*); };  void Bird_Repr(Bird *bird, char* buf) {     char owner_repr[80];     bird->owner->Repr(bird->owner, owner_repr);     sprintf(buf, \"&lt;Bird: name='%s' owner=%s>\",             bird->name, owner_repr); }  void Bird_Str(Bird *bird, char* buf) {     sprintf(buf, \"%s\", bird->name); }  Bird *New_Bird(char *name, Person *owner) {     Bird *bird = malloc(sizeof(Bird));     bird->name = name;     bird->owner = owner;     bird->Repr = Bird_Repr;     bird->Str = Bird_Str;      bird->object.offset_repr = offsetof(Bird, Repr);     bird->object.offset_str = offsetof(Bird, Str);      bird->object.Print_Repr = Object_Print_Repr;     bird->object.Print_Str = Object_Print_Str;      return bird; }  void Del_Bird(Bird *bird) {     free(bird); }  int main(void) {     Person *person = New_Person(\"Oleg\", \"Olegov\");     Bird *bird = New_Bird(\"Kukushka\", person);      \/*      \"\u0421\u043c\u043e\u0442\u0440\u0438\u043c\" \u043d\u0430 \u043e\u0431\u044a\u0435\u043a\u0442 person \u043a\u0430\u043a \u043d\u0430 Object      \u0438 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0441 \u044d\u0442\u0438\u043c \u0436\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u043c.      \u0412 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0435, \u043d\u0438\u043a\u0442\u043e \u043d\u0435 \u0437\u0430\u043f\u0440\u0435\u0449\u0430\u0435\u0442 \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c person      \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0431\u0435\u0437 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u0438\u044f \u0442\u0438\u043f\u0430:                ((Object*)person)->Print_Repr(person);       GCC \u044d\u0442\u043e \u0441\u0445\u0430\u0432\u0430\u0435\u0442, \u043d\u043e \u0432\u044b\u043a\u0438\u043d\u0435\u0442 warning.      *\/     ((Object*)person)->Print_Repr((Object*)person);<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-334946","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/334946","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=334946"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/334946\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=334946"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=334946"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=334946"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}