Be careful about implicit type conversion

Source: Internet
Author: User

C ++ contains many implicit rules. If you have no idea about these implicit rules, you may not find any problems. implicit type conversion is one of them.
(1) single argument constructor may cause implicit type conversion.
For example, you have a class:
Class myclass
{
Char * szbuffer;
Public:
Myclass ()
{
Szbuffer = NULL;
}
Myclass (INT size)
{
Szbuffer = new char [size];
Memset (szbuffer, 0, size );
}
~ Myclass ()
{
Delete [] szbuffer;
}
....
};
It seems understandable to write a constructor with an int parameter to specify the buffer size of the initial allocation, but if you or others accidentally write the following sentence:
// Myclass test;
Tested = 256;
Then the compiler will first use "myclass (INT size)" to construct a temporary object, and then call the value assignment operator to assign this temporary object to the test object. Even if you carefully write the value assignment operator (const myclass & operator = (const myclass & other) for myclas, the situation is also bad because the content of the test object is cleared.
If you do not write the value assignment operator, the situation will be messy! The compiler will use the default value assignment operation, that is, the bitwise copy, and the temporary object will be released after the value assignment operation is completed, and the destructor of the temporary object will be called, at this time, the szbuffer members of the temporary object and the szbuffer members of the test point to the same memory, so the subsequent operations on the szbuffer members of the test will be unpredictable.
To avoid this problem, C ++ introduces the explicit keyword and modifies the function name: explicit myclass (INT size), "test = 256; this sentence cannot be compiled.

(2) assignment operators can also cause type conversion.
For example, if myclass has a member function "const myclass & operator = (INT t)", then "test = 256;" can be compiled again, however, this method is rarely seen in practice.
 
(3) Use the class type conversion function with caution (User-Defined Conversion Function)
When using MFC, It is very convenient to use cstring and crect. If the function parameter is lpctstr (const char *) or lprect (rect *), the object can still be passed in. This is because both the cstring and crect classes have corresponding operators. Type conversion functions are sometimes very convenient. For example, there is a class:
Class mycolor
{
Public:
Unsigned char A, R, G, B;

Operator unsigned int () const
{
Return (A <24) | (r <16) | (G <8) | B;
}
};
Mycolor is used to store the components A, R, G, and B of A color value respectively. It is a common operation to convert an instance of this class into a 32-bit value, therefore, it is easy to use such a member function:
Mycolor test;
Unsigned int A = test;
 
If you write such a sentence to compare the size of each component, be careful:
Mycolor test;
Mycolor Test2;
Bool B = test> Test2;
Although mycolor does not write "operator>", this sentence can still be compiled and run. The compiler performs implicit type conversion on test and Test2 after function overload parsing, and finds the matching among all> operators. "Test> Test2" actually converts both test and Test2 into unsigned int, and then compares them!

References:
C ++ primer Stanley B. Lippman, Joseph Lajoie, translated by Pan aimin and Zhang Lili
Translation of Objective C ++ Scott Meyers
Nicola M. josutis, translated by Hou Jie and Meng Yan, C ++ standard library
 

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.