C ++ software development specifications-Part 2)

Source: Internet
Author: User

Assert is not a hasty macro. In order not to cause any difference between the Debug and Release versions of the program, assert should not produce any side effects. So assert is not a function, but a macro. Programmers can regard assert as a harmless testing method that can be safely used in any system status. If the program terminates at assert, it does not mean that the function containing this assert has an error, but the caller has an error. assert can help us find the cause of the error.

 

☆[ Rule 4.5-1] Use assertions to capture illegal situations that should not occur. Do not confuse the differences between illegal situations and error situations, the latter is inevitable and must be handled;

☆[ Rule 4.5-2] Check the validity (validity) of parameters using assertions at the function entrance );

☆[ Suggestion 4.5-1] When writing a function, you should repeat it and ask yourself: "What assumptions do I plan to make ?" Once the assumptions are identified, the assumptions should be checked using assertions;

☆[ Recommended 4.5-2] Generally, textbooks encourage programmers to design error prevention, but remember that such a programming style may conceal errors. When an error prevention design occurs, if the "impossible to happen" event does occur, Use assertions to trigger an alarm.

4.6 Comparison Between Reference and pointer

References are a concept in C ++. Beginners can easily confuse references with pointers. In the following program, n is a reference of m, and m is a referent ).

Int m;

Int & n = m;

N is equivalent to M alias (nickname). Any operation on N is an operation on M. Therefore, n is neither a copy of M nor a pointer to M. In fact, n is m itself.

Some referenced rules are as follows:

(1) The reference must be initialized at the same time (the pointer can be initialized at any time );

(2) there cannot be a null reference, and the reference must be associated with a valid storage unit (the pointer can be null );

(3) once the reference is initialized, the reference relationship cannot be changed (the pointer can change the object at any time ).

In the following example, K is initialized as a reference of I. Statement K = J cannot be changed to a reference of J, but the value of K is changed to 6. Since K is an I reference, the I value is also changed to 6.

Int I = 5;

Int J = 6;

Int & K = I;

K = J; // The values of K and I are changed to 6;

The above program looks like playing a text game and does not reflect the value of reference. The main function of the reference is to pass the parameters and return values of the function. In C ++, function parameters and return values are transmitted in three ways: value transfer, pointer transfer, and reference transfer.

The following is a sample program for "value transfer. Since X in the func1 function is a copy of the external variable N, changing the value of X does not affect N, so the value of N is still 0.

Void Func1 (int x)

{

X = x + 10;

}

...

Int n = 0;

Func1 (n );

Cout <"n =" <n <endl; // n = 0

The following is an example program for "pointer passing. Since x in the Func2 function is a pointer to the external variable n, changing the content of this pointer will change the value of n, so the value of n is 10.

Void Func2 (int * x)

{

(* X) = (* x) + 10;

}

...

Int n = 0;

Func2 (& n );

Cout <"n =" <n <endl; // n = 10

 

The following is a sample program for "reference transfer. Since x in the Func3 function is referenced by the external variable n, x and n are the same thing, changing x equals to changing n, so the value of n becomes

10.

Void Func3 (int & x)

{

X = x + 10;

}

...

Int n = 0;

Func3 (n );

Cout <"n =" <n <endl; // n = 10

 

Comparing the above three examples, we will find that the nature of "Reference transfer" is like "pointer transfer", and the writing method is like "value transfer ".

5 Appendix

5.1 variable Type Definition

Type
Rules
Example
 
Bool (BOOL)
Start with B
BIsParent
 
Byte (BYTE)
Start with
ByFlag
 
Short (SHORT)
Start with n
NFileLen
 
Int (INT)
 
Start with n

NStepCount

Long (LONG)

Start with l

LSize

Char (CHAR)

Start with ch

ChCount

Unsigned short (WORD)

Start with w

WLength

Unsigned long (DWORD)

Start with dw

DwBroad

Void (VOID)

Start with v

VVariant

String ending with 0

Start with sz

SzFileName

LPCSTR (LPCTSTR)

Start with str
StrString
 
HANDLE (HINSTANCE)
Starting with h
HHandle
 
Struct
Start with blk
BlkTemplate
 
BYTE *
Starting with pb
PbValue
 
WORD *
Starting with pw
PwValue
 
LONG *
Start with pl
PlValue
 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.