默认内置构造函数和非参数化用户创建的默认构建器之间是否有区别?
为什么这项工作:
#include<iostream>
using namespace std;
class student{
int rollNumber;
int age;
public :
student(){
}
};
int main(){
student const s;
}
但这不起作用?
#include<iostream>
using namespace std;
class student{
int rollNumber;
int age;
};
int main(){
student const s;
}
在这两种情况下,都有一个默认构造函数。
顶部具有用户定义的默认构造函数,底部具有内置默认构造函数。
默认内置构造函数和非参数化用户创建的默认构造函数之间是否有区别?
在第二个中,内置默认构造函数是否与用户定义的非参数构造函数相同?
有没有人知道为什么会发生这种情况?
Why does this work:
#include<iostream>
using namespace std;
class student{
int rollNumber;
int age;
public :
student(){
}
};
int main(){
student const s;
}
But this does not work?
#include<iostream>
using namespace std;
class student{
int rollNumber;
int age;
};
int main(){
student const s;
}
In both cases, there is a default constructor.
The top one has a user-defined default constructor, and the bottom one has the built-in default constructor.
Is there any difference between the default built-in constructor and a non-parameterized user-created default constructor?
In the second one, will the built-in default constructor be the same as the user-defined non-parameterized constructor?
Is there anyone who knows why this is happening?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
C ++具有 const-defeault-default-constructible 类型( [dcl.init.general] P8 ) 。它要求,如果默认的初始化const-qualified类型
t
,则必须是可构建的。对于类,它必须具有用户提供的构造函数,或者每个成员必须具有默认成员初始化器或可构建本身。
这就是为什么第一个示例编译(
student(){}
是用户提供的),第二个示例失败(int rollnumber;
and code> andint age; 没有初始化器)。
至于原因,答案是“标准说这么说”。 [fiff.dcl] p4 :
自C ++ 98以来一直是这样。
虽然我同意这似乎对限制太严格了(如果所有成员都可以变异怎么办?如果您想阅读单个成员的默认成员初始化器,而其他成员会使其他成员启用?如果您想构建虚拟对象怎么办?不过,要把所有东西都传递给其他所有东西吗? const学生&amp; S = _s; 基本上具有相同的效果。
这是编译器提供的默认构造函数和显式
Student(){}
之间的区别之一。还有一些是:noexcept
ness-自动确定,甚至可能更多。这些是用户提供的和编译器生成的构造函数之间的差异,这是const-default-constructiblintibles似乎是默认构造函数唯一独特的构造。
C++ has a concept of const-default-constructible types ([dcl.init.general]p8). It requires that if a const-qualified type
T
is default initialized, it must be const-default-constructible.For classes, it must have a user-provided constructor, or every member must have a default member initializer or be const-default-constructible itself.
This is why the first example compiles (
student(){}
is user provided), and the second one fails (int rollNumber;
andint age;
don't have an initializer).As for the why, the answer is "the standard says so". The rationale for this is given in [diff.dcl]p4:
And it's been that way since C++98.
Though I agree that this seems like too strict of a restriction (What if all the members are mutable? What if you want to read the default member initializer of a single member, leaving the others uninitialized? What if you want to construct a dummy object to pass to something else with everything uninitialized?) In all cases though, you can do something like
student _s; const student& s = _s;
for basically the same effect.This is one of the differences between the compiler provided default constructor and an explicit
student() {}
. A few more are:noexcept
-ness is automatically determineddelete
dAnd probably some more. These are the differences between user-provided and compiler generated constructors in general, being const-default-constructible seems to be the only one unique to default constructors.