C ++ is C's optimization

zhaozj2021-02-16  133

1. In C , we advocate use const char * const authorname = "scott meyers"; this way, for the pointer us to constrain the pointer itself, but also constantly quantizes the variables referred to by the pointer. This is why the above do.

2. Confusion caused by Define:

#define max (a, b) ((a)> (b)? (a): (b))

INT A = 5, b = 0;

Max ( a, b); // a value increased by 2 times

MAX ( a, b 10); // a value only increases 1 time

The inline function solved this problem:

Template

Inline Const T & Max (Const T & A, Const T & B)

{RETURN A> B? a: b;}

3. String * StringArray1 = static_cast (Malloc (10 * sizeof (string)));

String * stringArray2 = new string [10];

As a result, StringArray1 does point to sufficient space that can accommodate 10 String objects, but these objects are not created in memory. Moreover, if you don't jump from this obscure symbol (see the description of clauses M4 and M8), you have no way to initialize the objects in the array. In other words, StringArray1 is actually not used. Instead, StringArray2 points to an array containing 10 fully constructive String objects, each of which can be safely used in any read string.

It is also a bad idea to mix new and delete with Malloc and Free. Call free with a pointer for NEW, or call Delete to call with Malloc, which is unpredictable. Everyone knows "unpredictable" means that it may work well in the development phase, work well during the test phase, but may also be in the face of your most important customer's face.

If you call

New

Used

[]

,transfer

Delete

Also use

[]

. If called

New

Be useless

[]

, That call

Delete

Don't use it

[]

.

转载请注明原文地址:https://www.9cbs.com/read-8774.html

New Post(0)