编辑: wtshxd 2019-07-07
The C++ Programming Language Lecture 1:From C to C++ Brief Introduction&Requirements Coding Style Requirements -- Naming Conventions Hungarian Notation[GlobalPrefix][TypePrefix]-BaseTag-Name[GlobalPrefix]C++ member variables start with: m_Global variables start with: g_Static data members start with: s_Classes start with: CInterfaces start with: IStandard [Prefix]:p C A pointer.

CHAR* psz;

rg C An array. DWORD rgType[…];

pp C A pointer to a pointer. int* ppTop;

h C A handle. HANDLE hFile;

ref C A reference to. char Func(string& refResult);

Coding Style Requirements -- Naming Conventions (cont.) Standard "BaseTag"void -> v int -> iBOOL -> f UINT -> uiBYTE -> b CHAR -> chDouble -> d float-> fl WCHAR -> wch ULONG -> ulLONG -> l DWORD -> dwHRESULT -> hr fn -> functionsz -> NULL str USHORT, SHORT, WORD -> wName C Meaningful and Capitalizing every wordsExamplem_pdTopOfStackCTypeLibg_fSwitchForDests_szStartingURL Coding Style Requirements -- Naming Conventions (cont.) Let's have some tries…A flag indicates whether a C++ object has been initialized:BOOL m_fInitialized;

A Session ID:DWORD dwSessionID;

A Pointer to BYTE buffer:BYTE* pbBuffer;

A global buffer to store logfile filename:CHAR g_szLogFile[MAX_NUM];

A pointer to a global logfile name:CHAR* g_pszLogFile;

Coding Style Requirements -- Code Indentation

4 spaces per indent level Class CExample{ public:member functions declaration Cexample(Cexample(private:ifdef _DEBUG void TraceValue( int iValue endif … };

While (foo)if (NULL == bar)Func1(ad)else Func2(bc)Func3();

} Braces in Statements Useful Concepts Review Reviewing C in C++ int functionA(int iX, int iY);

int functionA(int iX, int iY) {};

functionA(3,5);

The first is a declaration, the second is a definition (with null statements), the third is an invokingDeclaration of a variable or function can appear many times, while definition only onceiX & iY - parameters,

3 &

5 - arguments Reviewing C in C++ (cont.) int iX;

extern int iX;

int functionB();

extern int functionB();

Keyword 'extern' means it is only a declaration, its definition is later or external to this file, asking the compiler not to assign memory or generate codeextern int functionB(invalid Reviewing C in C++ (cont.) int funcA(){ static int iX = 0;

} iX is a "local global variable" to the fileIts scope is only in this file Function can "remember " iX between callsBut iX is unavailable outside the function, its scope is in this function static int iX = 0;

int funcA( Reviewing C in C++ (cont.) int iX = 1024, iY = 4096;

int &rX = iX;

int* pi = &rX;

rX = iY++;

Reference rX could be regarded as an alias or another name of iXCould rX represent iY later? --- No!pi = &iY;

//---Only a pointer could do that Reviewing C in C++ (cont.) int Swap(int iX, int iY);

int Swap(int* pX, int* pY);

int Swap(int &iX, int &iY);

The first two are passing by value, the last is passing by referenceThe first could not modify the value of arguments outside of Swap, while the last two could Reviewing C in C++ (cont.)

3 Reasons to use passing by reference:To modify the objects that passed in the functionTo avoid the cost of copying large objectsTo have more than one return values struct Matrix { double a[10000][10000] };

int f1(Matrix m);

int f2(Matrix& m);

bool add_even(int a, int b, int& c){ if (a%2 ==

0 && b%2 == 0)c = a+b;

return true;

} return false;

} Reviewing C in C++ (cont.) The inner mechanism of reference and pointer may be similar, but they are quite different in usageUsing Reference when you find It always represents a non-null objectAnd it will not represent any other objectsUsing Pointer otherwisePointer could be re-assigned values, while reference could not Reviewing C in C++ (cont.) int f(int& ri){ if (ri = 0) return 0;

下载(注:源文件不在本站服务器,都将跳转到源网站下载)
备用下载
发帖评论
相关话题
发布一个新话题