After studying it, I was barely able to integrate it.
Writing C into OOP is quite fun.
Compiling environment: xcode3.2.3 + gcc4.2
/* <Br/> * cloud. h <br/> * cloudooc <br/> * created by cloud on 2010/8/21. <br/> * Copyright 2010 orz. all rights reserved. <br/> */<br/> # ifndef object_h <br/> # define object_h <br/> typedef void (* execute) (struct object * a_this); <br/> typedef struct object * (* allocate )(); <br/> typedef struct _ object_vtable <br/>{< br/> char * Name; <br/> execute EXE; <br/>} object_vtable; <br/> typedef struct _ basecmd <br/>{< br/> object_vtable vtable; <br/>} basecmd; <br/> // --- for common --- // <br/> basecmd * New (allocate newobj); <br/> void Delete (void * item ); <br/> // ------------------------ // <br/> // --- for basecmd --- // <br/> basecmd * newbasecmd (); <br/> void exe_basecmd (basecmd * a_this ); <br/> // ------------------------ // <br/> // --- for homecmd --- // <br/> typedef struct _ homecmd <br/> {<br/> object_vtable vtable; <br/>} homecmd; <br/> basecmd * newhomecmd (); <br/> void exe_homecmd (homecmd * a_this ); <br/> // -------------------------- // <br/> # endif
/* <Br/> * cloud. c <br/> * cloudooc <br/> * created by cloud on 2010/8/21. <br/> * Copyright 2010 orz. all rights reserved. <br/> */<br/> # include "cloud. H "<br/> // --- for common --- // <br/> basecmd * New (allocate newobj) <br/>{< br/> basecmd * OBJ = newobj (); <br/> return OBJ; <br/>}< br/> void Delete (void * item) <br/>{< br/> free (item ); <br/>}< br/> // ------------------------ // <br/> // --- for basecmd --- // <br/> basecmd * newbasecmd () <br/> {<br/> basecmd * cmd = malloc (sizeof (basecmd); <br/> cmd-> vtable. name = "Cloud"; <br/> cmd-> vtable.exe = exe_basecmd; <br/> return cmd; <br/>}< br/> void exe_basecmd (basecmd * a_this) <br/> {<br/> printf ("Do base command = % s/n", a_this-> vtable. name); <br/>}< br/> // ------------------------ // <br/> // --- for homecmd --- // <br/> basecmd * newhomecmd () <br/>{< br/> homecmd * cmd = malloc (sizeof (homecmd); <br/> cmd-> vtable. name = "home"; <br/> cmd-> vtable.exe = exe_homecmd; <br/> return cmd; <br/>}< br/> void exe_homecmd (homecmd * a_this) <br/> {<br/> printf ("Do home command = % s/n", a_this-> vtable. name); <br/>}< br/> //--------------------------//
# Include <stdio. h> <br/> # include <stdlib. h> <br/> # include "cloud. H "<br/> int main (INT argc, const char * argv []) <br/>{</P> <p> basecmd * cmd = new (newbasecmd ); <br/> cmd-> vtable.exe (CMD); <br/> Delete (CMD); </P> <p> basecmd * cmd2 = new (newhomecmd ); <br/> cmd2-> vtable.exe (cmd2); <br/> Delete (cmd2); </P> <p> return 0; <br/>}< br/>