Your SlideShare is downloading. ×
OOP in C - Before GObject (Chinese Version)
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Saving this for later?

Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime - even offline.

Text the download link to your phone

Standard text messaging rates apply

OOP in C - Before GObject (Chinese Version)

1,484
views

Published on

Published in: Self Improvement

0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,484
On Slideshare
0
From Embeds
0
Number of Embeds
10
Actions
Shares
0
Downloads
48
Comments
0
Likes
3
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

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