Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ctor: Why does 'explicit' prevent assignment construction?

I've got a class ByteArray defined like this:

class ByteArray
{
public:
    explicit ByteArray( unsigned int uiSize = 0 );
    explicit ByteArray( const char * ucSource );
    ByteArray( const ByteArray & other );

    ByteArray & operator=( const char * ucSource );
    ByteArray & operator=( const ByteArray & other );
}

While almost everything works, constructing a ByteArray by assignment doesn't compile.

ByteArray ba1( 5 );     // works
ByteArray ba2( ba1 );   // works
ByteArray ba3( "ABC" ); // works
ByteArray ba4;          // works
ba4 = "ABC";            // works
ByteArray ba5 = "ABC";  // <<<----- doesn't compile!

The compiler gives me a Cannot convert 'const char *' to 'ByteArray'.
However, the "assignment-constructor" should be the same as the copy-constuktor, ie. the ba5 line should compile just as the ba3 line--- in contrast to the construction of ba4 and subsequent assignment. So, I'm not quite sure what problem the compiler is having.

I know that a solution would be to remove the explicit in front of the 3rd ctor. I would rather understand what's going on first, though...

Edit:
The answer states it nicely: ByteArray ba5 = "ABC"; would get compiled as ByteArray ba5( ByteArray("ABC") ); --- NOT as ByteArray ba5("ABC"); as I thought it would. Obvious, but sometimes you need someone to point it out. Thanks everyone for your answers!

Why use 'explicit' anyway? Because there is an ambiguity between unsigned int and const char *. If I call ByteArray ba( 0 ); both ctors would be able to handle that, so I need to forbid the implicit conversion and make it explicit.

like image 586
Robin Avatar asked Jun 24 '14 07:06

Robin


People also ask

What is implicit constructor in C++?

implicit constructor is a term commonly used to talk about two different concepts in the language, the. implicitly declared constructor which is a default or copy constructor that will be declared for all user classes if no user defined constructor is provided (default) or no copy constructor is provided (copy).

Which of the following are true about conversion constructor in C++?

Conversion constructor in C++? It has some unique property like, its name will be same as class name, it will not return any value etc. The constructors are used to construct objects of a class.

What are copy constructors in C++?

Copy Constructor in C++ A copy constructor is a member function that initializes an object using another object of the same class. In simple terms, a constructor which creates an object by initializing it with an object of the same class, which has been created previously is known as a copy constructor.


1 Answers

ByteArray ba5 = "ABC"; is copy initialization, not assignment.

Think of it as

ByteArray ba5(ByteArray("ABC"));

or at least that's what the compiler sees. It's illegal in your case because of the explicit property of the constructor - the compiler wants to use that conversion constructor to perform copy initialization, but it can't because you didn't explicitly use it.

like image 193
Luchian Grigore Avatar answered Oct 11 '22 15:10

Luchian Grigore