Polymorphism/C: Difference between revisions
Updated formatting to use <syntaxhighlight>.
(Moved Polymorphism C example to here) |
(Updated formatting to use <syntaxhighlight>.) |
||
(3 intermediate revisions by 3 users not shown) | |||
Line 1:
{{Programming-example-page|Polymorphism}}
#define FN_PTR(x) (x)
{
Point* Point_new0()
{
▲ /* After reading this you may understand */
▲ /* why Bjarne Stroustrup's invented C++ */
▲ #if defined( _WIN32 ) || defined( MSC_VER )
▲ #else
}
▲ #define FN_PTR(x) (x)
▲ #endif
Point* Point_new1(int x0)
▲ typedef struct Point
Point* pthis = malloc( sizeof( Point ) );
▲ int x;
pthis->x =
▲ void (*dtor)(); /* virtual */
▲ void (*print)(); /* virtual */
▲ } Point;
}
}
void
{
{
free(*pthis); *pthis
▲ pthis->y = 0;
▲ pthis->dtor = FN_PTR(Point_dtor);
▲ pthis->print = FN_PTR(Point_print);
}
}
memcpy(pthis, p, sizeof( Point ) );
int
▲ if(pthis && *pthis)
(*pthis)->dtor();▼
▲ Point* pthis = (Point*) malloc( sizeof( Point ) );
▲ memcpy(pthis, p, sizeof( Point ) );
▲ pthis->dtor = FN_PTR(Point_dtor);
▲ pthis->print = FN_PTR(Point_print);
▲ int Point_getY(Point* pthis) { return pthis->y; }
▲ int Point_setX(Point* pthis, int x0) { pthis->x = x0; }
▲ int Point_setY(Point* pthis, int y0) { pthis->y = y0; }
▲ void Point_print() { printf("Point\n"); }
▲ void Point_dtor() {}
{
struct _Circle
{
Point point;
▲ Point point;
▲ int r;
▲ } Circle;
{
if(pthis && *pthis)
{
}
}
}</syntaxhighlight>
|