There are two classes: A and B.

If objects of the same class are copied, the copy constructor is called and copies the corresponding fields. But if you produce this:

A a; B b; a = (A)b; 

How does copying work in this case?

  • one
    What are A and B ? In general, (A)b simply impossible. Accordingly, there will be no copying here - the code is simply incorrect. - AnT

1 answer 1

If in class B there is an available type conversion operator from a class B type to a class A type, then it is called in a casting expression of type (A)b , the result of which is a temporary object of type A This temporary object is assigned to object a using a copy or move assignment operator in a sentence.

 a = (A)b; 

Here is a demo program.

 #include <iostream> struct A { A & operator =( const A & ) { std::cout << "A::operator =" << std::endl; return *this; } }; struct B { operator A() const { std::cout << "operator B::A()" << std::endl; return A(); } }; int main() { A a; B b; a = ( A )b; return 0; } 

Its output to the console

 operator B::A() A::operator = 

The conversion operator can also be declared with the explicit function specifier. For example,

 explicit operator A() const; 

Or a more “tangled” example of transformation when the conversion operator is overloaded for lvalue and rvalue .

 #include <iostream> struct A { A() { std::cout << "A::A()" << std::endl; } A( const A & ) { std::cout << "A::A( const A & )" << std::endl; } A & operator =( const A & ) { std::cout << "A::operator =( const A & )" << std::endl; return *this; } A & operator =( A && ) { std::cout << "A::operator =( A && )" << std::endl; return *this; } }; struct B { explicit operator A() const & { std::cout << "operator A() &" << std::endl; return A(); } explicit operator A() const && { std::cout << "operator A() &&" << std::endl; return A(); } }; int main() { A a; B b; a = ( A )b; std::cout << std::endl; a = ( A )B(); return 0; } 

The output of this program to the console

 A::A() operator A() & A::A() A::operator =( A && ) operator A() && A::A() A::operator =( A && ) 

For example, for the sentence

 a = ( A )B(); 

the conversion operator is called for the rvalue temporary object created by the B() call B() ,

 operator A() && 

Then a temporary object of type A is created inside this statement.

 A::A() 

This temporary object is assigned to object a by a moving assignment operator.

 A::operator =( A && ) 

The easiest option is when in class A there is a conversion constructor. For example,

 #include <iostream> struct A { A() {} explicit A( const struct B & ); A & operator =( const A & ) { std::cout << "A::operator =( const A & )" << std::endl; return *this; } }; struct B { }; A::A( const B & ) { std::cout << "A::A( const B & )" << std::endl; } int main() { A a; B b; a = ( A )b; return 0; } 

Output of the program to the console

 A::A( const B & ) A::operator =( const A & ) 

In the definition of class A a transformative constructor is declared.

 explicit A( const struct B & ); 

which is called when casting in the expression ( A )b

And finally, if class B is derived from class A , then you can simply write

 a = b; 

In this case, an object of class B implicitly converted to an object of class A and a copying assignment operator of class A called.

  • Thanks for the detailed response) - Alerr
  • @Alerr Not at all. See my updated answer. - Vlad from Moscow