Convert by Constructors

The class X β†’ is converted to Y in two ways: 1) by constructors and 2) transformation functions.
I realized that a single argument constructor is used for the conversion.

In the specification:

An implicitly declared copy constructor is not an explicit constructor; it can be called to implicitly convert types.

Question:

So, this means that the conversion uses not only one argument constructor, but also a copy constructor ?. If so, what scenario is used ?. any sample code snippet?

Please carry me if the question is very important.

+5
source share
4 answers

, copy-ctor, T(const T&) T(T&).

n3337 par 12.8 ++.

8 X X:: X (const X &), - B X , const B & const volatile B & - X, M ( ), , const M & const volatile M &.119 X:: X (X &)

c-tor ,

struct So
{
};

int main()
{
    So s = So();
}

copy-ctor , , So s((So()));

+1

, :

struct A {};
A a;
A b = a;

, . , : explicit A( A const & ) {} .

+2

The implicit copy constructor is the one that the compiler writes for you. He always has the form

T(const T&);

This means that any object that has a conversion to operator const T&can be implicitly copied, even if this is not what you wanted. The most common way to call this is to make a copy of the base class from the derived class; this is called a slice of objects because the copy will not be of the same type as the original and is likely to lose some important properties or behavior.

+1
source

All Articles