Developing Yed objects - Inheriting the only interface from a Yed object
Yed scenario allows inheritance of the only object interface, similar to inheritance from an 'abstract class' in C++ scenario. Using the same example introduced in 'Inheriting all entities from a Yed object' paragraph, let's see how.
This is Myclass object declaration:
typedef struct {
char PRIVATE(protected);
PUBLIC int (*My_Func)(void *);
PUBLIC char my_attr;
} Myclass;
Assuming Myclass is an object without any method implementation, as an 'abstract class'. Cderiv will inherit the only Myclass interface:
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 is the same:
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;
}
Also the destructor:
void _DMyClass(void *pvT) {
free(pvT);
}
In this case, the method My_Func, related to Myclass object in terms of interface, does not have any implementation. Thus, we build a simple implementation of it:
/**** FUNCTION PROTOTYPES ( in Cderiv header file declaration ) ****/
int My_Func(void *);
...
...
/**** METHOD IMPLEMENTATION ( in Cderiv file definition ) ****/
int My_Func(void *pVWork) {
Cderiv *this=pvWork; // 'THIS' POINTER OF THE INSTANCE!!!
printf("The protected member value is [%d]\n", this->PRIVATE(protected));
return 0;
}
In this case, pointer 'this' is related to Cderiv object, because the implementation of this method is specific to Cderiv object ( Myclass has declared the only interface ).
The example of code using Cderiv is the same:
#include "yedstd.h"
#include "cderiv.h"
main(){
Cderiv *cFoo;
cFoo=New(Cderiv);
if(cFoo!=NULL) {
cFoo->My_Func(cFoo); // interface from Myclass, implementation from Cderiv
cFoo->Foo_Func(cFoo); // method specific of Cderiv
}
Delete(Cderiv,cFoo);
}