Explicit-implicit Conversions

Source: Internet
Author: User

Although C ++ is a strongly typed language, it is not as strong as Java and C # because too many implicit conversions are allowed.

Implicit conversion between basic types inherited from C Language
Implicit conversion of T * pointer to void *
Non-explicit constructor accepts implicit conversion of a parameter.
Implicit conversion from subclass to base class (secure)
Implicit conversions of the same type from const to non-const (secure)
Apart from the five implicit conversions mentioned above, the C ++ compiler is still very clever. When direct implicit conversions are not available, it tries indirect implicit conversions, this makes implicit conversions very subtle sometimes. A misuse will be accepted by the compiler and unexpected results will occur. For example, assume that Class A has a non-explicit constructor, the only parameter is Class B, and Class B also has a non-explicit constructor that accepts type C, when trying to initialize class A with an instance of type C, the compiler finds that the process is not directly constructed from Type C. However, because Class B can be accepted, type C can be implicitly converted to type B, so the path from C-> B-> A is smooth. This implicit conversion is not a problem in most cases, but it is not what we want, because it may cause some subtle bugs that are hard to capture.

 

The explicit constructor in C ++ illustrates how to use an explicit constructor to prevent implicit conversion using two small examples of C ++.

There is a simple plural class:

Class clxcomplex
{
Public:
Clxcomplex (double dreal = 0.0, double dimage = 0.0) {m_dreal = dreal; dimage = dimage ;}

Double getreal () const {return m_dreal ;}
Double getimage () const {return m_dimage ;}

PRIVATE:
Double m_dreal;
Double m_dimage;
};
We know that the following three lines of code are equivalent:

Clxcomplex lxtest = 2.0;
Clxcomplex lxtest = clxcomplex (2.0 );
Clxcomplex lxtest = clxcomplex (2.0, 0.0 );
In fact, for the first two lines, the compiler converts them into 3rd lines of code. Because we have written the constructor, the compiler performs implicit conversion based on our constructor, and directly converts a double value to a clxcomplex object. However, sometimes we do not want to perform implicit conversion, or implicit conversion may cause errors. For example, the following simplified string class:

Class clxstring
{
Public:
Clxstring (INT ilength );
Clxstring (const char * pstring );
~ Clxstring ();

PRIVATE:
Char * m_pstring;
};

Clxstring: clxstring (INT ilength)
{
If (ilength> 0)
M_pstring = new char [ilength];
}

Clxstring: clxstring (const char * pstring)
{
M_pstring = new char [strlen (pstring)];
Strcpy (m_pstring, pstring );
}

Clxstring ::~ Clxstring ()
{
If (m_pstring! = NULL)
Delete m_pstring;
}
We can use the length of the string to initialize a clxstring object, but we do not want to see the following code:

Clxstring lxtest = 13; // equivalent to clxstring lxtest = clxstring (13 );
This will cause unnecessary ambiguity for reading the code.
Also, we know that the following code uses string a to initialize a clxstring object:

Clxstring lxtest = "A"; // equivalent to clxstring lxtest = clxstring ("");
However, if someone writes:

Clxstring lxtest = 'a'; // equivalent to clxstring lxtest = clxstring (65 );
The above code initializes a string with a length of 65 (ASCII code value of the letter A, which is stored in ASCII values in C and C ++.
Of course, none of the above is what we want to see. We need to use the display constructor at this time.
Declare the constructor to explicit to avoid implicit conversion.
The following is the clxstring used to display the constructor:

Class clxstring
{
Public:
Explicit clxstring (INT ilength );
Clxstring (const char * pstring );
~ Clxstring ();

PRIVATE:
Char * m_pstring;
};
In this case, to initialize a clxstring object with the string length, you must display the call constructor:

Clxstring lxtest = clxstring (13 );
The following code cannot be compiled.

Clxstring lxtest = 13;
Clxstring lxtest = 'a ';
 

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.