如何正确使用转换构造函数? [英] How to correctly use converting constructors?

查看:38
本文介绍了如何正确使用转换构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读到 C++ 编译器能够在提供拟合转换构造函数或操作数时隐式转换类型.我实际上找到了看起来很像这样的示例代码:

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:

要求从‘const char [6]’转换为非标量类型‘Dog’狗 d = "流浪者";

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

编译时我添加了编译器选项-std=c++11,所以应该不是C++版本,对吧?
我在互联网上找到的示例(至少对我而言)看起来完全相同,所以我不知道这里出了什么问题.
例如,我对这个主题的输入来自这个视频:转换构造函数和重载运算符 - 最新的大多数

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

推荐答案

您还需要了解您正在使用复制初始化而不是直接初始化.

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

它们是不同的,你需要了解它们是如何与 explicit 的关系.您需要了解转换链的工作原理,最多涉及一个用户定义的转换.

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";

d2 案例尝试将文字转换为 Dog,然后将其复制(移动)到 d2.但这将是一个 double 转换:const char*string,然后 stringDog.

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.

d1 case 构造 d1 将参数传递给构造函数,因此只需将 const char* 转换为 string.(在这两种情况下,将 const char [6] 提升为 const char* 也在那里,但不计入允许的唯一一个",处于不同的类别.)

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.)

复制初始化没有指定rover"作为构造函数的参数.它说这里有一些东西,但是这里需要一个 Dog".这里是 copy-init 声明语法的右侧,不是任何可识别的函数.编译器必须找到合法的转换.

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.

这篇关于如何正确使用转换构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆