Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to correctly use converting constructors?

Tags:

I read that C++ compilers are able to implicitly convert types, when fitting converting constructors or operands are provided. I actually found example code that looks much like this:

class Dog{     private:        string name;     public:         Dog(string n):name(n){} //This as the converting constructor }  int main(){     Dog d = "rover"; } 

Whenever I run this code the compiler throws an error message:

conversion from ‘const char [6]’ to non-scalar type ‘Dog’ requested Dog d = "rover";

When compiling I add the compiler option -std=c++11, so it shouldn't be about the C++ version, right?
Examples I found on the internet (at least to me) look quite identical, so I have no clue of what is going wrong here.
My input about this topic comes for example from this video: Convert constructor and overloading operators - thenew moston

like image 957
Julian Avatar asked Jun 07 '16 07:06

Julian


People also ask

How can we use constructor to convert types?

Conversion constructor in C++? The constructors are used to construct objects of a class. Sometimes constructors may take some arguments, or sometimes it does not take arguments. When a constructor takes only one argument then this type of constructors becomes conversion constructor.

What is a converting constructor?

A conversion constructor is a single-parameter constructor that is declared without the function specifier explicit . The compiler uses conversion constructors to convert objects from the type of the first parameter to the type of the conversion constructor's class.

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

In C++, if a class has a constructor which can be called with a single argument, then this constructor becomes conversion constructor because such a constructor allows automatic conversion to the class being constructed.

What is the point of constructors in C++?

C++ Constructors. Constructors are methods that are automatically executed every time you create an object. The purpose of a constructor is to construct an object and assign values to the object's members. A constructor takes the same name as the class to which it belongs, and does not return any values.


1 Answers

You also need to understand that you are using copy initialization rather than direct initialization.

They are different, and you need to understand how, and their relationship with explicit. You need to understand how chains of conversions work, with at most one user-defined conversion involved.

Dog d1 ("rover"); Dog d2 = "rover"; 

The d2 case tries to convert the literal to a Dog, and then copy (move) that to d2. But that would be a double conversion: const char* to string and then string to Dog.

The d1 case constructs d1 passing the argument to the constructor, so only one conversion const char* to string. (In both cases, promoting const char [6] to const char* is in there too but doesn't count toward the "only one" allowed, being in a different category.)

The copy-initialization does not specify "rover" as an argument of the constructor. It says "here is something, but a Dog is needed here". here is the right-hand-side of the copy-init declaration syntax, not any identifiable function. The compiler than has to find a legal conversion.

In the direct-init case, you are simply giving parameters for a function (a constructor). The compiler converts what you gave it into the declared argument type.

like image 55
JDługosz Avatar answered Sep 23 '22 15:09

JDługosz