C++ 中的默认与隐式构造函数

2021-12-30 00:00:00 constructor c++

这很简单,但是捷克语(我的母语)不区分隐式和默认,所以我对一些捷克语翻译感到困惑,隐式和默认构造函数或构造函数调用之间的区别是什么.

This is very trivial, but Czech language (my native) doesn't distinguish between implicit and default, so I am confused by some Czech translations what is the difference between implicit and default constructor or constructor call.

struct Test {
  Test(int n=0) { }
};

你能用这些术语描述什么是:

Can you describe in these terms what is:

  1. 测试 t1;
  2. 测试 t2();
  3. 测试 t3 = 3;
  4. 测试 t4(4);
  5. 测试 t5 = 测试(5);

?

推荐答案

default 和 implicit 这两个术语,在谈论构造函数时有以下含义:

The terms default and implicit, when talking about a constructor have the following meaning:

  • 默认构造函数 是可以不带参数调用的构造函数.它要么不接受任何参数,要么为所采用的每个参数提供默认值.

  • default constructor is a constructor that can be called with no arguments. It either takes no arguments or has default values for each of the arguments taken.

隐式构造函数是一个术语,通常用于谈论语言中的两个不同概念,

implicit constructor is a term commonly used to talk about two different concepts in the language, the

  • 隐式声明的构造函数,这是一个默认或复制构造函数,如果没有提供用户定义的构造函数(默认)或没有提供,将为所有用户类声明提供了复制构造函数(copy).也就是说,一个没有用户声明构造函数的类有一个默认构造函数隐式声明.

  • 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). That is, a class with no constructors declared by the user has one default constructor implicitly declared.

隐式定义的构造函数是一个隐式声明的构造函数,它被使用(odr-used1在语言中并且编译器将为其提供定义.

implicitly defined constructor is a implicitly declared constructor that is used (odr-used1 in the language and for which the compiler will provide a definition.

 

struct test
{
    test(int i = 0) { }
    // test(test const&) implicitly declared here
};

struct test2 { };      // implicitly declared: test2(), test2(test2 const&)

int main()
{
    test t;

    test copy(t);      // causes *definition* of the implicitly
                       // declared copy constructor

    test2 t2;          // causes *definition* of test2::test2()

    test2 copy2(t2);   // causes *definition* of test2::test2(test2 const&)
}

简单来说,一个构造函数是默认,如果它可以不带参数调用.如果构造函数不是由用户提供而是声明/定义的,则它是隐式(声明/定义).

In simple terms, a constructor is default if it can be called with no arguments. A constructor is implicit(ly declared/defined) if it is not provided by the user but declared/defined.

截至具体情况:

Test t1;

使用默认构造函数,Test(int = 0),它不是隐式的.

Uses the default constructor, Test(int = 0), which is not implicit.

Test t2();

这是该语言的一个奇怪的怪癖,它声明了一个不带参数的函数并返回一个 Test 对象.

This is a strange quirk of the language, it declares a function that takes no arguments and returns a Test object.

Test t3 = 3;

这称为复制初始化,相当于从3Test*转换的组合code> 和从转换结果复制 t3 的构造.这将使用Test(int) 构造函数进行转换,然后使用隐式定义(和声明)复制构造函数.注意:编译器可以优化掉副本,但它必须验证复制构造函数是否可用(访问说明符)并且可以被定义.

This is called copy-initialization and is equivalent to the composition of an implicit* conversion from 3 to Test and copy construction of t3 from the result of the conversion. This will use the Test(int) constructor for the conversion, and then the implicitly defined (and declared) copy constructor. Note: the compiler can optimize away the copy, but it must verify that the copy constructor is available (access specifiers) and can be defined.

Test t4(4);

使用 Test(int) 构造函数,在本例中它不充当 默认 构造函数.

Uses the Test(int) constructor, which in this case is not acting as a default constructor.

Test t5 = Test(5);

等效于 Test t3 = 3 的情况,唯一的区别是在这种情况下从 5Test 的转换是显式的.在这个例子中,这无关紧要,但如果构造函数被标记为 explicit,这一行会编译,而 t3 情况会失败.

Equivalent to the Test t3 = 3 case, with the only difference that the conversion from 5 to Test is explicit in this case. In this example it won't matter, but if the constructor had been marked as explicit this line would compile while the t3 case would fail.

*) 隐式的另一种用法,在这种情况下指的是从 3Test 的转换没有被明确请求在代码中.将此与程序员显式请求转换的 t5 进行比较:Test(5).

*) Yet another use of implicit, in this case referring to the fact that the conversion from 3 to Test is not explicitly requested in the code. Compare this with t5 where the conversion is explicitly requested by the programmer: Test(5).

相关文章