Successfully reported this slideshow.
Your SlideShare is downloading. ×

OOP in C - Before GObject (Chinese Version)

Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Loading in …3
×

Check these out next

1 of 18 Ad
Advertisement

More Related Content

Slideshows for you (20)

Advertisement

OOP in C - Before GObject (Chinese Version)

  1. 1. OOP in C Before GObject [email_address] elpam@Taiwan, Taipei, 2008
  2. 2. LICENSE <ul><li>本投影片授權方式為: </li><ul><li>姓名標示─非商業性─相同方式分享 3.0 台灣版
  3. 3. http://creativecommons.org/licenses/by-nc-sa/3.0/tw/ </li></ul></ul><ul><li>所有的範例程式皆為 Public Domain </li></ul>
  4. 4. About This Slides <ul><li>All example was build by </li><ul><li>GCC-4.1.3
  5. 5. GLIB-1.2.10
  6. 6. GMAKE-3.81 </li></ul></ul>
  7. 7. Agenda <ul><li>Constructor Review in C++
  8. 8. Constructor in C </li></ul>
  9. 9. C++ Language Review <ul><li>Constructor </li></ul><ul><ul><li>parent's constructor
  10. 10. member value initialize
  11. 11. initial member function (x)
  12. 12. do something </li></ul></ul>class Dog: public Animal{ private : string name; public: Dog(); }; Dog::Dog( const string n) : Animal() /*(1)*/ , name(n) /*(2)*/ { /* (4) */ cout << '' 汪汪 '' << endl; }
  13. 13. C++ Constructor <ul><li>Which one? </li><ul><li>cba
  14. 14. abc </li></ul></ul>/* 3-1.cpp */ class A{ public: A(){printf( “a” );}; }; class B: public A{ public: B(){printf( “b” );}; }; class C: public B{ public: C(){printf( “c” );}; }; C* c = new C;
  15. 15. C++ Constructor (cont') <ul><li>Answer: </li><ul><li>abc </li></ul><li>Why parent's constructor execute first ? </li><ul><li>在child的Constructor之中可能會Access到Parent的member value (及 member function) </li></ul></ul>
  16. 16. C++ Default Constructor <ul><li>Which one? </li><ul><li>abc
  17. 17. abC
  18. 18. ABC </li></ul></ul>/* 3-2.cpp */ class A{ public: A(){printf( “a” );}; A( int ){printf( “A” );}; }; class B: public A{ public: B(){printf( “b” );}; B( int ){printf( “B” );}; }; class C: public B{ public: C(){printf( “c” );}; C( int ){printf( “C” );}; }; C* c = new C(1);
  19. 19. C++ Default Constructor (cont') <ul><li>Answer: </li><ul><li>abC </li></ul><li>在沒有指定的情況之下Compiler會使用預設的Constructor </li><ul><li>也就是沒有參數的那一個 </li></ul><li>如果將A:A()拿掉的話會發生什麼事? </li></ul>/* 3-2.cpp */ class A{ public: A(){printf( “a” );}; A( int ){printf( “A” );}; }; class B: public A{ public: B(){printf( “b” );}; B( int ){printf( “B” );}; }; class C: public B{ public: C(){printf( “c” );}; C( int ){printf( “C” );}; }; C* c = new C(1);
  20. 20. Write Constructor Accurately constructor( Value ) : parent's constructor , member value initialize { /* do something */ } /* 3-3.hpp */ class A{ public: A(){ va = 0 ; }; A( int v){ va = v; }; private: int va; }; class B: public A{ public: B(); B( int ); private: int vb; }; /* ex3.cpp */ B::B() : A() , vb( 0 ) { printf( '' hello this is B() '' ); B::B( int v) : A( v ) /* !! */ , vb( v ) { printf( '' hello this is B(int) '' );
  21. 21. Constructor 該做的事情 <ul><li>parent's constructor
  22. 22. member value initialize
  23. 23. do something </li></ul>constructor( Value ) : parent's constructor , member value initialize { /* do something */ }
  24. 24. From C++ to C
  25. 25. Constructor in C <ul><li>we use blahblah_new to replace malloc </li><ul><li>命名方式並沒有語法上的限制 </li></ul></ul>struct A{ int a; int b; }; struct A* A_new() { struct A* ret = malloc( sizeof (A) ); ret -> a = 0; ret -> a = 1; return ret; }
  26. 26. Constructor in C (cont') <ul><li>Constructor </li><ul><li>parent's constructor
  27. 27. member value initialize
  28. 28. do something </li></ul></ul>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; }
  29. 29. Constructor's Properties <ul><li>不用宣告即有Virtual的屬性 </li><ul><li>執行繼承關係裡最下層的Constructor </li></ul><li>Child的Constructor執行之前,Parent的Constructor 應該要先被執行 </li><ul><li>上面的範例需要手動的檢查,才能正確的執行parent's constructor </li></ul></ul>
  30. 30. Constructor's Properties (cont') <ul><li>Drawback: Parent's constructor can't be reused! </li><ul><li>You need to rewrite the code what parent do </li><ul><li>Initialize parent's member value
  31. 31. Initialize parent's member function!?
  32. 32. ............. </li></ul><li>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. </li><ul><li>Let's why we use Gobject to do this </li></ul></ul></ul>
  33. 33. Conclusion <ul><li>如果需要比較復雜的繼承關係,可以選擇GObject所提供的介面來完成
  34. 34. Gobject並不是使用C來完成C++/OOP裡 全部 的功能
  35. 35. 我們將在下一個投影片之中導入GObject for OO </li></ul>
  36. 36. V-Table <ul><li>Actually, above concept is coming from the implementation of V-Table (Virtual-Function-Table, Dispatch Table).
  37. 37. You can find out more information via reading OO Language's compiler implementation or some JIT interpreter's implementation. </li><ul><li>http://en.wikipedia.org/wiki/Vtable </li></ul></ul>

×