内置类型有默认构造函数吗? [英] Do built-in types have default constructors?

查看:174
本文介绍了内置类型有默认构造函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读文章后,我指出 int()产生0,因为临时 int 是初始化的值,而不是因为 int()调用 int 。 (根据我的理解,这篇文章有缺陷。)



我也说过原始(内置)类型没有构造函数。原作者要求我检查Section $ 10.4.2(TC ++ PL),它说


内置类型也有默认构造函数但是我仍然认为语句C ++允许甚至内置类型(原始类型)有默认的构造函数。



我认为Bjarne在TC ++ PL中混淆了构造函数,如() 与实际构造函数调用。价值初始化没有介绍,当时Bjarne正在写这本书,对吧?那么,TC ++ PL中的文本是不正确的,因为C ++ 98和C ++ 03?



你们认为什么?



EDIT



我问Bjarne个人(通过邮件)了解TC ++ PL中有缺陷的文本是他的回复


我认为你把实际构造函数调用与概念上有一个构造函数混在一起。



>解决方案

构造函数是一个成员函数(构造函数在C ++标准的第12节中充分说明,它包含特殊成员函数,如构造函数和析构函数)。



成员函数只能为类类型定义(C ++ 03 9.3 / 1说在类的定义中声明的函数,不包括用友元说明符声明的那些函数,称为成员函数类)。因此,非类类型(包括基本类型,数组类型,引用类型,指针类型和枚举类型)没有构造函数。



我没有C ++编程语言的副本读取内置类型也有默认构造函数的引用上下文,但我猜测Stroustrup是在松散的非技术意义上使用术语构造函数,或者术语的含义或在标准中使用的方式在书籍发布之后,语言标准化。我猜想前者比后者更有可能。


After reading this article I made a point that int () yields 0 because the temporary int is value initialized and not because int() calls the default constructor for int. (The article is flawed according to my understanding.)

I also said that primitive (built-in) types don't have constructors. The original author asked me to check Section $10.4.2 (TC++PL) which says

Built-in types also have default constructors ($6.2.8)

But I still think that the statement "C++ allows even built-in type (primitive types) to have default constructors." is flawed (as per C++03).

I think Bjarne in TC++PL has mixed up "constructor like notation i.e ()" with actual constructor call. Value initialization were not introduced at that time when Bjarne was writing the book, right? So is the text in TC++PL incorrect as per C++98 and C++03 ?

What do you guys think?

EDIT

I asked Bjarne personally (via mail) regarding the flawed text in TC++PL and this was his reply

I think you mix up "actual constructor calls" with conceptually having a constructor. Built-in types are considered to have constructors (whatever words the standard use to describe their behavior).

解决方案

A constructor is a member function (constructors are fully specified in clause 12 of the C++ Standard, which covers special member functions like constructors and destructors).

A member function can only be defined for a class type (C++03 9.3/1 says "Functions declared in the definition of a class, excluding those declared with a friend specifier, are called member functions of that class").

So non-class types (including fundamental types, array types, reference types, pointer types, and enum types) do not have constructors.

I don't have a copy of The C++ Programming Language to read the context of the quote that "Built-in types also have default constructors," but I would guess that Stroustrup is either using the term "constructor" in a loose, non-technical sense, or the meaning of the term or the way in which it is used in the Standard changed between when the book was published and when the language was standardized. I'd guess the former is far more likely than the latter.

这篇关于内置类型有默认构造函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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