为什么显式允许默认构造函数和具有 2 个或更多(非默认)参数的构造函数?

2023-09-26C/C++开发问题
2

本文介绍了为什么显式允许默认构造函数和具有 2 个或更多(非默认)参数的构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我知道带有一个(非默认)参数的构造函数就像隐式转换器一样,从参数类型转换为类类型.但是,explicit 可用于限定任何构造函数,无参数(默认构造函数)或具有 2 个或更多(非默认)参数的构造函数.

I understand that constructors with one (non-default) parameter act like implicit convertors, which convert from that parameter type to the class type. However, explicit can be used to qualify any constructor, those with no parameters (default constructor) or those with 2 or more (non-default) parameters.

为什么在这些构造函数上允许显式?是否有任何示例说明这有助于防止某种隐式转换?

Why is explicit allowed on these constructors? Is there any example where this is useful to prevent implicit conversion of some sort?

推荐答案

一个原因当然是因为它没有伤害.

One reason certainly is because it doesn't hurt.

需要它的一个原因是,如果您有第一个参数的默认参数.构造函数变为默认构造函数,但仍可用作转换构造函数

One reason where it's needed is, if you have default arguments for the first parameter. The constructor becomes a default constructor, but can still be used as converting constructor

struct A {
  explicit A(int = 0); // added it to a default constructor
};

C++0x 将它实际用于多参数构造函数.在 C++0x 中,初始化列表可用于初始化类对象.哲学是

C++0x makes actual use of it for multi parameter constructors. In C++0x, an initializer list can be used to initialize a class object. The philosophy is

  • 如果您使用 = { ... },那么您将使用某种复合值"初始化对象,该复合值"在概念上表示对象的抽象值,并且您想要已转换为类型.

  • if you use = { ... }, then you initialize the object with a sort of "compound value" that conceptually represents the abstract value of the object, and that you want to have converted to the type.

如果使用 { ... } 初始化程序,则直接调用对象的构造函数,不一定要指定转换.

if you use a { ... } initializer, you directly call the constructors of the object, not necessarily wanting to specify a conversion.

考虑这个例子

struct String {
    // this is a non-converting constructor
    explicit String(int initialLength, int capacity);
};

struct Address {
    // converting constructor
    Address(string name, string street, string city);
};

String s = { 10, 15 }; // error!
String s1{10, 15}; // fine

Address a = { "litb", "nerdsway", "frankfurt" }; // fine

通过这种方式,C++0x 表明 C++03 的决定,允许在其他构造函数上显式,根本不是一个坏主意.

In this way, C++0x shows that the decision of C++03, to allow explicit on other constructors, wasn't a bad idea at all.

这篇关于为什么显式允许默认构造函数和具有 2 个或更多(非默认)参数的构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

无法访问 C++ std::set 中对象的非常量成员函数
Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)...
2024-08-14 C/C++开发问题
17

从 lambda 构造 std::function 参数
Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)...
2024-08-14 C/C++开发问题
25

STL BigInt 类实现
STL BigInt class implementation(STL BigInt 类实现)...
2024-08-14 C/C++开发问题
3

使用 std::atomic 和 std::condition_variable 同步不可靠
Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)...
2024-08-14 C/C++开发问题
17

在 STL 中将列表元素移动到末尾
Move list element to the end in STL(在 STL 中将列表元素移动到末尾)...
2024-08-14 C/C++开发问题
9

为什么禁止对存储在 STL 容器中的类重载 operator&()?
Why is overloading operatoramp;() prohibited for classes stored in STL containers?(为什么禁止对存储在 STL 容器中的类重载 operatoramp;()?)...
2024-08-14 C/C++开发问题
6