Static

xiaoxiao2021-03-06  49

The extraction of the problem:

TypedEf List cmediafilelist; // Video file linked list, cmediafile is a definition

CMEDIAFILIST :: Iterator it = m_mediafilelist.begin ();

For (; it! = m_mediafilelist.end (); it )

{

XTrace ("full path file name:% s, channel number:% D, record number:% D, / N",

(static_cast ((* it))). Spathname.c_str (),

(static_cast (* it))). NChannel,

(static_cast ((* IT))). UID

);

}

// ..........

/ / The above code makes a simple test, outputs all the contents of the list.

// ..........

CMEDIAFILIST :: Iterator it = m_mediafilelist.begin ();

Xtrace ("File Name of Full Path:% S", (static_cast (* it))). Spathname.c_str ());

// ..........

Look at the results,

The second TRACE output is empty.

It seems that there is nothing wrong, huh, I am also debugging. Said from the following programs, it is best to compile:

#include

"cono.h"

#include

"iostream"

Using

Namespace std;

Class CMYCLASS

{

PUBLIC:

CMYCLASS ()

{

Cout <<

"CMYCLASS :: CMYCLASS ()" << ENDL;

}

~ CMYCLASS ()

{

Cout <<

"CMYCLASS :: ~ CMYCLASS ()" << ENDL;

}

Void Draw ()

{

Cout <<

"CMYCLASS :: DRAW ()" << Endl;

}

}

INT_TMain

Int argc, _tchar * argv [])

{

CMYCLASS * POBJECT

=

New CMYCLASS;

Static_cast (* pObject) .draw ();

DELETE POBJECT;

_Getch ();

Return

0;

}

// The output is as follows:

CMYCLASS :: CMYCLASS () CMYCLASS :: ~ cmyclass () CMYCLASS :: ~ CMYCLASS ()

Take a look at the code,

STATIC_CAST Do you feel some disaster! What is the reason, because automatic construction and destructuring are in type conversion

CMYCLASS

Object. Why don't we have a destructure output?

Because the type conversion structure here uses a copy constructor.

In general, if the class does not specially declare the function, the system automatically generates four functions:

Structure

2. Destructure

3. Copy the constructor (note)

4. Overload = operator, roughly CMYCLASS OPERATOR = (const cmyclass & object) {............} The above did not define copy constructor, of course, will not output:

CMYCLASS :: CMYCLASS ()

.

Below we are from defining a copy constructor (add as a member function):

CMYCLASS (Const CMYCLASS & OBJECT)

{

Cout <<

"CMYCLASS :: CMYCLASS (Const CMYCLASS & Object) << Endl;

}

Look at the output:

CMYCLASS :: CMYCLASS :: Draw () CMYCLASS :: ~ cmyclass () CMYCLASS :: ~ cmyclass ()

Oh, this time is like we are willing.

Do you have an optimized place? Use references and pointer efficiency.

Static_cast (pobject) -> DRAW ();

or

Static_cast (* pObject) .draw ();

There is only one output (there is no copy constructor and destructure), as follows:

CMYCLASS :: CMYCLASS () CMYCLASS :: Draw () CMYCLASS :: ~ CMYCLASS ()

Now let's go back to see: Why do you like this? What is the benefit?

I use (CMYCLASS &) .DRAW (); work is not very good? ? ?

Static_cast: Usually uses it,

Function is basically the same as the type of C style, the meaning is the same

Reinterpret_cast:

Used to explain one type of value as another type of value, rarely used

Const_cast:

Remove a const or volatileness property of an object or pointer

Dynamic_cast:

The target type of the conversion is limited to the pointer or reference of the class, and the class that is converted must be a polymorphism.

It is something introduced in C99, and there are at least 2 benefits:

First, it is an annotation that you can immediately know that this is the type conversion immediately when you see these keywords in your code. Second, the type conversion in the C language is usually difficult to search, and the keyword CAST can easily find the type conversion in the program.

Mandatory type conversion

Programmers can use three forced type conversion expressions in expressions:

1 (t) e; // C language, if (int) x;

2 t (e); // C language used in the style, similar to function call, such as int (x);

3 Cast_operator (e); // C language provides new forms of four types of conversion

Everyone can explore more in practice, I am also a rookie, huh, huh. Egg, stone @% $ # @ ^% # @ ^ $, I flash ~

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

New Post(0)