C ++中的默认与隐式构造函数 [英] Default vs. Implicit constructor in C++
问题描述
这是非常微不足道的,但捷克语(我的本地)不区分隐含和默认,所以我感到困惑的一些捷克翻译隐含和默认构造函数或构造函数调用之间的区别。
struct Test {
对象。
Test(int n = 0){}
};你可以用这些术语描述:
- 测试t1;
- 测试t2();
- 测试t3 = 3;
- 测试t4(4);
- 测试t5 =测试(5);
$ b b
解决方案术语默认当谈论构造函数具有以下含义:
默认构造函数调用没有参数。
隐式构造函数是常用于说话的术语在语言中的两个不同概念,
隐式声明构造函数如果未提供用户定义的构造函数(默认)或没有提供复制构造函数(复制),则将为所有用户类声明的默认或复制构造函数。也就是说,没有用户声明的构造函数的类有一个默认构造函数隐式声明。
是在语言中使用( odr-used 1 )的隐式声明构造函数,编译器将为其提供定义。
struct test
/ pre>
{
test(int i = 0){}
// test(test const&)implicitly declaration here
};
struct test2 {}; //隐式声明:test2(),test2(test2 const&)
int main()
{
test t;
test copy(t); //造成*定义*的隐式
//声明的拷贝构造函数
test2 t2; //造成test2 ::的定义* test2()
test2 copy2(t2); //导致test2 :: test2(test2 const&)的定义*
}
简单来说,构造函数是 default 没有参数。
在特定情况下,如果构造函数不是由用户提供,而是声明/定义的, :
测试t1;
使用默认构造函数
Test(int = 0)
,这不是隐式的。测试t2
这是一个奇怪的奇怪的语言,它声明一个函数不接受参数, code> Test
这称为复制初始化,相当于从
3
到转换 c 测试
并复制t3
。这将使用Test(int)
构造函数进行转换,然后使用隐式定义的(和构造函数。注意:编译器可以优化拷贝,但必须验证拷贝构造函数是否可用(访问说明符)并可以定义。Test t4(4);
使用
Test(int)
构造函数,测试t5 =测试(5) ;
等效于
在代码中未明确请求从Test t3 = 3
,唯一的区别是在这种情况下从5
到Test
的转换是显式的。在这个例子中它没有关系,但如果构造函数被标记为explicit
,这行将编译而t3
3
到Test
的转换。与t5
进行比较,其中转换由程序员明确请求:Test(5)
。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:
- Test t1;
- Test t2();
- Test t3 = 3;
- Test t4(4);
- Test t5 = Test(5);
?
解决方案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
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.
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.
As of the specific cases:
Test t1;
Uses the default constructor, Test(int = 0)
, which is not implicit.
Test t2();
This is a strange quirk of the language, it declares a function that takes no arguments and returns a Test
object.
Test t3 = 3;
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);
Uses the Test(int)
constructor, which in this case is not acting as a default constructor.
Test t5 = Test(5);
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.
*) 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)
.
这篇关于C ++中的默认与隐式构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!