Unfortunately, you cannot do this.
First let me explain the obvious.
Suppose we have:
class Y; class X { X(const Y&); }; void Z(const X&);
This tells the compiler "given Y , we can make X ", so the code looks like this:
Y y; Z(y);
It works, the compiler does this:
Y y; Z(X(y));
An explicit ban on this.
So, if you have:
class X { explicit X(const Y&); };
Then
Z(y);
Will not work! Since you must explicit construct the X construct from Y
This is called a conversion constructor, the idea is that you can freely convert. This is great when you have something that expects string and give it a const char* , since it implicitly builds the string and passes it, you are βsufferingβ from the interaction of the main types.
source share