Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implicit copy constructor

Quote from n3337 12.3.1/3

A non-explicit copy/move constructor (12.8) is a converting constructor. An implicitly-declared copy/move constructor is not an explicit constructor; it may be called for implicit type conversions.

Quote from ANSI ISO IEC 14882 2003

A non-explicit copy-constructor (12.8) is a converting constructor. An implicitly-declared copy constructor is not an explicit constructor; it may be called for implicit type conversions.

I have no ideas, how copy-constructor can be used for implicit type conversions. And if it's misprint/error in standard, why it's not corrected since C++03 standard? Any links and examples (if we can use it for type conversions) are really appreciated.

like image 333
ForEveR Avatar asked Sep 14 '12 16:09

ForEveR


1 Answers

A copy constructor can convert from an object of a derived type by slicing it:

struct A {};
struct B : A {};

B b;
A a = b; // uses A::A(A const&) to convert B to A
like image 168
Mike Seymour Avatar answered Sep 28 '22 09:09

Mike Seymour