Developing Yed objects - Complete inheritance from a Yed object
Assume that we want to create a new object, call Cderiv, that inherits interface and implementation from our Myclass. First of all, modify entity order to follow first inheritance rule in Yed scenario:
typedef struct {
char PRIVATE(protected);
PUBLIC int (*My_Func)(void *);
PUBLIC char my_attr;
} Myclass;
Cderiv inherits from Myclass and moreover has its own methods and attributes:
typedef struct{
// FROM Myclass declaration -----------
char PRIVATE(protected);
PUBLIC int (*My_Func)(void *);
PUBLIC char my_attr;
// Declaration of Cderiv entities -----
int PRIVATE(siFoo);
PUBLIC int (*Foo_Func)(void *);
} Cderiv;
The constructor of Cderiv contains the constructor of Myclass and moreover has its own code:
Cderiv * _Cderiv(void) {
Cderiv *this=malloc(sizeof(Cderiv));
if(this!=NULL) {
memset(this,0,sizeof(Cderiv));
// INIT from Myclass constructor -----------
this->My_Func=My_Func;
this->PRIVATE(protected)=24;
// INIT for Cderiv entities -----------
this->PRIVATE(siFoo)=32;
this->Foo_Func=Foo_Func;
}
return this;
}
The same thing for the destructor; in this case, no particulat operation is requested for releasing the object, then a normal 'free' is sufficient; otherwise, as we have done in the constructor, we must insert the destructor code of Myclass, because Cderiv, inheriting from Myclass, shares the same entities with it.
void _DMyClass(void *pvT) {
free(pvT);
}
The method My_Func, related to Myclass object, has been defined as PRIVATE_FUNCTION. Following second rule of inheritance in Yed scenario, delete this attribute from method declaration and definition:
/**** FUNCTION PROTOTYPES ( in Myclass header file declaration ) ****/
int My_Func(void *);
...
...
/**** METHOD IMPLEMENTATION ( in Myclass file definition ) ****/
int My_Func(void *pVWork) {
MyClass *this=pvWork; // 'THIS' POINTER OF THE INSTANCE!!!
printf("The protected member value is [%d]\n", this->PRIVATE(protected));
return 0;
}
Blu line is the heart of inheritance mechanism: My_Func can be invoked either from an instance of Myclass object or from an instance of Cderiv object, because Myclass object exists in both Myclass and Cderiv objects declaration at the same offset memory:
Myclass declaration typedef struct { |
Cderiv declaration typedef struct { |
This is the reason why a Yed derived object can inherit from only one Yed object, and a Yed derived object can inherit from a Yed object that can be either base or derived.
An example of code using Cderiv:
#include "yedstd.h"
#include "cderiv.h"
main(){
Cderiv *cFoo;
cFoo=New(Cderiv);
if(cFoo!=NULL) {
cFoo->My_Func(cFoo); // method got from Myclass through inheritance
cFoo->Foo_Func(cFoo); // method specific of Cderiv
}
Delete(Cderiv,cFoo);
}