Research on heavy load of C ++ type conversion (I)

Source: Internet
Author: User

Recently, when using COM strings, I think the automatic type conversion of _ bstr_t is very interesting.

Below are some usage of _ bstr_t:

Assign the char */wchar * value to _ bstr_t

M_rst-> getfields ()-> getitem (ncol)-> value = (_ bstr_t) svalue;

Returns the char */wchar * value from _ bstr_t.

Strcpy (svalue, (char *) (_ bstr_t) vvalue );

Wcscpy (svalue, (wchar *) (_ bstr_t) vvalue );

 

Analyze the m_rst-> getfields ()-> getitem (ncol)-> value = (_ bstr_t) svalue;

It has two usage methods:

(_ Bstr_t) svalue
Indicates creating a temporary _ bstr_t object, which will cause the _ bstr_t (char *) constructor of the _ bstr_t class to be called.

Vvalue = (_ bstr_t)
Operator = (_ bstr_t) of vvalue will be called

My example

Class testaa
{
Int num;
Public:
Testaa (int)
{
Num =;
}
Testaa (long)
{
Num = (INT);
}
Testaa & operator = (int A) // assign a value to reload testaa AA (10); AA = (INT) 10;

{
Num =;
Return * this;
}
Testaa & operator = (long)
{
Num = (INT);
Return * this;
}
Operator long () // defines the forced type conversion of the testaa class. The usage is testaa AA (10); (long) AA;

{
Return (long) num;
}
};

Int _ tmain (INT argc, _ tchar * argv [])
{
Long A = 10;
Testaa AA (a); // call testaa (long A) to construct

A = 20;
AA = A; // call testaa & operator = (long)

Long lll = (long) AA; // call operator long () to force type conversion and return the value to lll

(Testaa) (long) 10; // call testaa (long A) to construct a temporary object

(Testaa) (long) AA; // call operator long () to force type conversion, call testaa (long A) to construct the return value, and create a temporary object.

Return 0;

}

 

In the following case, I picked an example from the Internet, which is similar to _ bstr_t. I would like to express my gratitude here.

 

# Include
<
Stdio. h
>


# Include
<
String
>



Class
Mydata


{

Public
:

Mydata (
Const

Int
Nvalue,
Const

Char

*
Pszvalue)

 

{

M_nvalue
=
Nvalue;

M_strvalue
=
Pszvalue;

}




 
Void
Set (
Const

Int
Nvalue,
Const

Char

*
Pszvalue)

 

{

M_nvalue
=
Nvalue;

M_strvalue
=
Pszvalue;

}




 
//
Reload type conversion


 
Operator

Int
()
//
Operator Int & ()


 

{

 
Return
M_nvalue;

}




 
Operator

Const

Char

*
()

 

{

Return
M_strvalue.c_str ();

}




 
Operator

Char

*
()

 

{

Return
Const_cast
<
Char
*>
(M_strvalue.c_str ());

}




Protected
:

 
Int
M_nvalue;

STD ::
String
M_strvalue;

}

;


Void
Printint (
Int
N)


{

Printf (
"
% D/n
"
, N );

}




Void
Printstring (
Const

Char

*
Psz)


{

 
If
(Psz
! =
Null)

Printf (
"
% S/n
"
, Psz );

}




Void
Print (
Int
N)


{

Printint (N );

}




Void
Print (
Char

*
P)


{

Printstring (P );

}




Void
Main (
Void
)


{

Mydata (
11
,
"
AA
"
);

Printint (mydata); // This will be implicitly converted, called (int
) Mydata



Printstring (mydata );

 
//
Print (mydata );
//
Compile Error
Because there are two prints, the compiler does not know which implicit type conversion to perform.


Print ((
Int
) (Mydata ));
//
Call print (int n)


Print ((
Char

*
) (Mydata ));
//
Call print (char * P)


}


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.