close

原文:
http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Access_Control

一個class與外界溝通的interface(廣義的interface)就是它的public member functions;而呼叫這些public member functions的地方我們可以稱之為client。當設計一個class時,重要的是該class的interface要完整且不要有多餘的public member functions,client用不到的member function就要將之設為private,以避免外界的接觸。

這麼作的原因是在於,減少外界的接觸就等於降低該class與client之間的耦合,將來該class需要改變時,也不需要修改太多client的程式。

class的data member必須設為private。如果client需要與該class的data member接觸,則必須透過accessor function。而這些accessor的definition通常放置在header file裡(implicit inline)。例外是static data member不需要被設為private。

若需要unit test該class時(使用Google Test),Google C++ Style Guide允許你將原本private access level的member functions提升至protected access level;如此一來就可以在你的testing code中測試這個class的member functions。

 

arrow
arrow
    文章標籤
    C++
    全站熱搜

    coherence 發表在 痞客邦 留言(0) 人氣()