метод, объявленный в базовом классе. • Конкретный код, который будет исполнен, зависит от реального класса объекта (не от типа указателя!). • В терминологии С++: «виртуальный метод».
**argv) { void *p; while (*++argv) { switch (**argv) { case ’p’: p = new(Point, 1, 2); break; case ’c’: p = new(Circle, 1, 2, 3); break; default: continue; } draw(p); move(p, 10, 20); draw(p); delete(p); } return 0; } Указатель на объект Конструирование объекта класса Point Конструирование объекта класса Circle Вызовы виртуальных методов Вызов виртуального деструктора!
иерархий классов. • Метаклассы! • Конструктор, деструктор, набор динамических методов — свойства метакласса. • Добавление еще одного динамического метода — добавление свойства, т.е. наследование!
иерархий классов. • Метаклассы! • Конструктор, деструктор, набор динамических методов — свойства метакласса. • Добавление еще одного динамического метода — добавление свойства, т.е. наследование! • Описания классов с одним и тем же набором методов — объекты метакласса.
создать новый класс Point, // с таким базовым, sizeof(struct Circle), // с таким размером объекта ctor, Circle_ctor, // с таким конструктором draw, Circle_draw, // … и деструктором 0); // конец списка static const struct Class _Circle = { sizeof(struct Circle), Circle_ctor, 0, Circle_draw }; const void *Circle = &_Circle; против ручного
sel, meth, ... 0); */ struct Class { const struct Object _; /* описание класса */ const char *name; /* имя класса */ const struct Class *super; /* базовый класс */ size_t size; /* размер объекта */ void *(*ctor)(void *self, va_list *app); void *(*dtor)(void *self); int (*differ)(const void *self, const void *b); int (*puto) (const void *self, FILE *fp); }; Базовый класс для всех метаклассов
decided that we want to create class descriptions, and we can write a destructor that silently prevents that a class description is destroyed. It may be quite useful to be able to compare and display class descriptions. However, this means that the metaclass Class has the same set of methods, and therefore the same type of description, as the class Object, i.e., the chain from objects to their class description and from there to the description of the class description ends right there. Properly initialized, we end up with the following picture: Class • "Class" Object sizeof Object make class return 0 compare display struct Class name super size ctor dtor differ puto Object • "Object" ? sizeof anObject make object return self compare display struct Class anObject • struct Object The question mark indicates one rather arbitrary decision: does Object have a superclass or not? It makes no real difference, but for the sake of uniformity we define Object to be its own superclass, i.e., the question mark in the picture is replaced by a pointer to Object itself. 6.4 Subclassing — Any Given the descriptions Class and Object, we can already make new objects and even a new subclass. As an example, consider a subclass Any which claims that
decided that we want to create class descriptions, and we can write a destructor that silently prevents that a class description is destroyed. It may be quite useful to be able to compare and display class descriptions. However, this means that the metaclass Class has the same set of methods, and therefore the same type of description, as the class Object, i.e., the chain from objects to their class description and from there to the description of the class description ends right there. Properly initialized, we end up with the following picture: Class • "Class" Object sizeof Object make class return 0 compare display struct Class name super size ctor dtor differ puto Object • "Object" ? sizeof anObject make object return self compare display struct Class anObject • struct Object The question mark indicates one rather arbitrary decision: does Object have a superclass or not? It makes no real difference, but for the sake of uniformity we define Object to be its own superclass, i.e., the question mark in the picture is replaced by a pointer to Object itself. 6.4 Subclassing — Any Given the descriptions Class and Object, we can already make new objects and even a new subclass. As an example, consider a subclass Any which claims that Примем, что базовым классом Object является Object
decided that we want to create class descriptions, and we can write a destructor that silently prevents that a class description is destroyed. It may be quite useful to be able to compare and display class descriptions. However, this means that the metaclass Class has the same set of methods, and therefore the same type of description, as the class Object, i.e., the chain from objects to their class description and from there to the description of the class description ends right there. Properly initialized, we end up with the following picture: Class • "Class" Object sizeof Object make class return 0 compare display struct Class name super size ctor dtor differ puto Object • "Object" ? sizeof anObject make object return self compare display struct Class anObject • struct Object The question mark indicates one rather arbitrary decision: does Object have a superclass or not? It makes no real difference, but for the sake of uniformity we define Object to be its own superclass, i.e., the question mark in the picture is replaced by a pointer to Object itself. 6.4 Subclassing — Any Given the descriptions Class and Object, we can already make new objects and even a new subclass. As an example, consider a subclass Any which claims that Примем, что базовым классом Object является Object нельзя удалять классы!
– базовый метакласс. • 4 базовых динамических метода: конструктор, деструктор, сравнение, запись в поток. • Нужно добавить новый метод (cool) — создаем новый метакласс, новый селектор (функция cool()) и функцию вызова метода базового класса (super_cool()).