Type conversion in C ++, static_cast, reinterpret_cast, boost: lexical_cast

Source: Internet
Author: User

In short, static_cast <> will try to convert, for example, float-to-integer, while reinterpret_cast <> simply changes the compiler's intention to reconsider that object as another type.

 

Float F = 123.4;
Int I = (INT) F; // correct
Int J = static_cast <int> (f); // correct
Int J = boost: lexical_cast <int> (f); // Error
Int J = boost: lexical_cast <int> (F * 10); // correct

 

Char ch [4] = "123 ";
Int BDL = static_cast <int> (CH [2]); // correct 51 (3 ASCII code): convert to the corresponding ASCII code
Int bjl = boost: lexical_cast <int> (CH [2]); // correct 3
Int bd1 = static_cast <int> (CH); // compilation error, cannot be converted like this
Int bj1 = boost: lexical_cast <int> (CH); // 123

==========================================

Static_cast:

ALRA: tcpmsg * ptcpmsg = (ALRA: tcpmsg *) wparam;

Float to integer
Pttopleft. Y = static_cast <int> (RDBMS. Height () * (1.0/5 ));

Converts a byte to an unsigned byte.
Assert (0x0f = static_cast <byte> (pdata [2]);

Static_cast <unsigned int> (static_cast <unsigned char> (iocem. szdata [I])

Retrieve the integer from the parameter:
Int ncommandid = static_cast <int> (wparam );

Convert an integer to a parameter type:
Static_cast <lparam> (ncommandid)
Static_cast <uint> (ncommandid)

Static_cast <wparam> (makeword (nmorsfibler, ncommroute), static_cast <lparam> (makewparam (nrorsport, makeword (nnewstaus, noldstaus ))

 

Get the class pointer and convert it to the specified class:
Cpagedeviceboxiopm * pparent = static_cast <cpagedeviceboxiopm *> (getparent ());
Return static_cast <cdocdevice *> (static_cast <cformdevice *> (pparent)-> getdocument ());

Transfer pointer type:
Afxbeginthread (alracommit: sendthread, static_cast <lpvoid> (pparam ));
ALRA: psendingthreadparam pparam = static_cast <ALRA: psendingthreadparam> (lparam );

----------------------------------------------------------------------------------

Reinterpret_cast:

Unsigned char * P = reinterpret_cast <unsigned char *> (pdata + 5 );

Passing message pointer parameters:
Postmessage (ALRA: um_send_a_package, reinterpret_cast <wparam> (pcardparam), 0 );
ALRA: psendingthreadparam pcardparam = reinterpret_cast <ALRA: psendingthreadparam> (wparam );

Convert a reference to a specific type:
ALRA: iocem & iocem = reinterpret_cast <ALRA: iocem &> (rpackage );
ALRA: iopm & iopm = reinterpret_cast <ALRA: iopm &> (rpackage );

Ret =: sqlprepare (hstmt, reinterpret_cast <uchar *> (strsql. getbuffer (0), SQL _nt );

Getparent ()-> postmessage (ALRA: um_device_card_selection_change,
Static_cast <wparam> (getdlgctrlid (), // pass the integer
Reinterpret_cast <lparam> (pcard); // pass pointer type

------------------------------------------------------------------------------

Boost: lexical_cast:

Convert the bytes type to the int type, and then convert it to the byte type. For example, vs [3] = '2' is converted to int 2, and then to byte, Which is ox02 0000 0010.
Makeword (static_cast <byte> (boost: lexical_cast <int> (vs [3]), static_cast <byte> (boost :: lexical_cast <int> (vs [2])
Convert string to double:
Boost: lexical_cast <double> (strvalue );
Convert the cstring variable associated with the Edit Control to the double type:
Boost: lexical_cast <double> (m_strmrlimit2.getbuffer ());

When there are no decimal places after X 10 in the following two items, the effect is the same. If there are decimal places, lexical_cast <int> will fail.
Iopm. nswitchlimit = static_cast <int> (boost: lexical_cast <double> (m_strsrswitchthreshold.getbuffer () * 10.0 );
Iocem. nchanged = boost: lexical_cast <int> (boost: lexical_cast <float> (this-> m_strchanged) * 10 );

STD: String S = lexical_cast <STD: String> (123456.7); // convert the floating point type to the string type.

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.