最近研讀Unit Test(單元測試)有感,記錄一下Unit Test的相關知識:

1. 什麼是Unit Test
2. Hello World of Unit Test in C\C++
3. 為什麼要做Unit Test
4. Unit Test的優缺點

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

原文:http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Default_Arguments
Default argument的好處在於,你不必再為了一些罕見的special case而另外寫一個function,直接使用default argument即可。而且default argument和function overloading比起來,default argument可以用更清楚的方式來分區必要的(required)和非必要的(optional)引數。
Default argument的壞處在於,和function pointer一起使用時會出問題:
typedef void (*PFN)(int);
void Foo(int a = 10)

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

原文:http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml#Function_Overloading
C++允許function overloading(C語言不允許),可以讓多個functions擁有一樣的名字,只要這些functions的parameters或return type不同。比方說:
class MyClass {
public:
    void Analyze(const string& text);

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

  • Mar 16 Sat 2013 22:37
  • sizeof

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

Google C++ Style Guide建議,盡量使用sizeof(varname),而非sizeof(type)。

比方說程式中有一個MyStruct;
struct MyStruct

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

不少公司的C\C++考題會考名詞解釋;通常在時間有限、答案卷篇幅有限的情況下,不太可能要你長篇大論去解釋太多細節的部分,主要是在考你是否理解這些基本觀念,只要寫個1-2行點到為止即可。

面試常考的名詞解釋:
static
extern
const

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

整理面試過常見的C\C++題目:

1. 基本觀念篇
常考名詞解釋
array
macros

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

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

C++並沒有定義interface這個關鍵字(Java和C#有定義interface關鍵字),在C++中只要有class或struct(class和struct的功能幾乎相同)符合下列條件,我們就可以稱之為interface:
* 所有的method都為pure virtual method或static method
* 不能有任何的non-static data members

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

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

C++中的class是value type by default(C#和Java中的class是reference type by default),在撰寫C++程式時,難免需要class instances之間的拷貝,Copy ctor和assignment operator就是用於這種場合。當你從一個現有的class instance來創建另一個class instance時,Copy ctor就會被呼叫。當你把一個現有的class instance用賦值給另一個class instance時,assignment operator就會被呼叫。copy ctor和assignment operator也常用於STL container之中。若沒有為class宣告copy ctor或assignment operator時,compiler會為你隱式生成
其實只有少數的class有被拷貝的需求,而compiler隱式生成的copy ctor和assignment operator常是bug的根源,也會造成performance上的問題(和call-by-reference比起來),而且copy ctor和assignment operator會降低程式的可讀性,因為它們不像一般的function一樣,必須顯式的去呼叫它;所以你往往難以trace到底哪些地方呼叫了copy ctor或assignment operator。所以Google C++ Style Guide規定只有當你有拷貝需求時才去實作copy ctor和assignment operator,若沒有這樣的需求;則必須使用goole自定義的macro DISALLOW_COPY_AND_ASSIGN來防止compiler隱式產生copy ctor和assignment operator,而DISALLOW_COPY_AND_ASSIGN不過就是copy ctor和assignment operator的宣告,你必須把它放在private access level裡面。
// A macro to disallow the copy constructor and operator= functions

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

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

在一個class中,規定declaration的順序為public → protected → private。這樣的規定通常是為了程式的可讀性,把越重要的部份擺在越前面,一個class的interface(廣義的interface)就是它與外界溝通的那些public member functions,所以將public section放在最前面,而只有該class內部會用到的data member或help functions自然就設為private並且放在最後面。

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

原文:
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。

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

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

若你的class沒有定義ctor時,compiler會自動隱式生成一個default ctor給你;當你的class中有定義data members時,compiler隱式生成的default ctor並不會給予你的data members適當的初值。
class Cls
{

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

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

規定程式中的non-local static object只能為POD,因為它們的ctor以及dtor的順序在C++中沒有被規範,這會造成難以解決的Bug;non-local static object為global或namespace scope的變數以及class中的static member data。
在同一個translation unit之中,global variables被初始化的順序即為它們被定義的順序。
class CFoo

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

1 2
Blog Stats
⚠️

成人內容提醒

本部落格內容僅限年滿十八歲者瀏覽。
若您未滿十八歲,請立即離開。

已滿十八歲者,亦請勿將內容提供給未成年人士。