Learning and sorting of implicit class type conversion in C + + primer


There is a saying in C + + Primer: the constructor that can be called with a single argument defines a conversion from the formal parameter type to the class type. This sentence is represented by the following code:

class A
  A(B b);// Constructor for a single argument
  func(A a);
A a;
B b;
a.func(b);// Func function should have accepted an argument of type A, but because of the existence of a special constructor, the parameter B of type B is converted into an object of type A with the help of this special constructor to complete the conversion. So this statement is correct

As can be seen from the above code segment, the constructor called by a single argument defines the conversion from class type to other types, and this conversion occurs implicitly. Here are several keywords: single argument, constructor and implicit conversion.

What happens in the process of implicit conversion? Object a has no member function with parameter type B, but it has a constructor with a single class B formal parameter, so there will be no error at compile time. For the statement a.func (b), the compiler will use this special constructor to generate a temporary object, and then call the normal func (a) function with the temporary object. The func (a) function ends and the temporary object is unregistered.

Is this conversion good? It varies by class and context! You always need it and don’t want it. The language has this function by default. However, we can also use the keyword explicit to prevent this invisible “optimization”! The explicit keyword can only be used on constructors, and only needs to be marked when the function is declared. It is not necessary to mark the keyword when the class function is defined.

In the above example, if implicit type conversion with constructor a (b) is prevented, the declaration of the constructor can be processed as follows:

explicit A (B b)

At this point, the statement A. func (b) is about to make an error, but we can explicitly use the constructor. Taking the above example, we can use the statement


Complete the same function, and implicit conversion is not involved at this time. A (b) generates a temporary A-type object and passes it to the func function call. Everything follows the rules without any implicit steps invisible to the programmer. The displayed constructor stops using the constructor implicitly. Any constructor can explicitly create a temporary object, which is its right, and the constructor modified by explicit is no exception.

For implicit class type conversion, the author of C + + primer has the following experience: generally, unless there is an obvious reason to define implicit conversion, the single parameter constructor should be explicit. Setting the constructor to explicit avoids errors, and users can explicitly construct objects when transformations are useful.

This is the end of this article on learning and sorting out implicit class type conversion in C + + primer. For more information about implicit class type conversion in C + +, please search for previous articles of developeppaer or continue to browse the relevant articles below. I hope you will support developeppaer in the future!