oop in c - before gobject (chinese version)
TRANSCRIPT
OOP in CBefore GObject
[email protected]@Taiwan, Taipei, 2008
LICENSE
3.0
http://creativecommons.org/licenses/by-nc-sa/3.0/tw/
Public Domain
About This Slides
All example was build by GCC-4.1.3
GLIB-1.2.10
GMAKE-3.81
Agenda
Constructor Review in C++
Constructor in C
C++ Language Review
Constructor
parent's constructor
member value initialize
initial member function (x)
do something
class Dog: public Animal{private: string name;public: Dog();};
Dog::Dog( const string n) : Animal() /*(1)*/ , name(n) /*(2)*/{ /* (4) */ cout a = 0; ret -> a = 1; return ret;}
Constructor in C (cont')
Constructorparent's constructor
member value initialize
do something
struct A{ int a;};struct B{ struct A parent; int a;};
struct A* A_new(){ struct A* ret = malloc( sizeof(A) ); ret -> a = 0; return ret;}
struct B* B_new(){ struct B* ret = malloc( sizeof(B) ); ret -> parent -> a = 0; /* ???? */ ret -> b = 0; return ret;}
Constructor's Properties
VirtualConstructor
ChildConstructorParentConstructor parent's constructor
Constructor's Properties (cont')
Drawback: Parent's constructor can't be reused!You need to rewrite the code what parent doInitialize parent's member value
Initialize parent's member function!?
.............
Yes, what we think is the same. Boring jobs should be prevent. Stupid codes should be removed. Or we'll always find out stupid bug.Let's why we use Gobject to do this
Conclusion
GObject
GobjectCC++/OOP
GObject for OO
V-Table
Actually, above concept is coming from the implementation of V-Table (Virtual-Function-Table, Dispatch Table).You can find out more information via reading OO Language's compiler implementation or some JIT interpreter's implementation. http://en.wikipedia.org/wiki/Vtable