默认内置构造函数和非参数化用户创建的默认构建器之间是否有区别?

发布于 2025-02-12 11:00:40 字数 553 浏览 0 评论 0原文

为什么这项工作:

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一生独一 2025-02-19 11:00:41

C ++具有 const-defeault-default-constructible 类型( [dcl.init.general] P8 ) 。它要求,如果默认的初始化const-qualified类型t,则必须是可构建的。

对于类,它必须具有用户提供的构造函数,或者每个成员必须具有默认成员初始化器或可构建本身。

这就是为什么第一个示例编译(student(){}是用户提供的),第二个示例失败(int rollnumber; and code> and int age; 没有初始化器)。

至于原因,答案是“标准说这么说”。 [fiff.dcl] p4 :

无法分配const对象,因此必须将其初始化以保持有用的值。

自C ++ 98以来一直是这样。

虽然我同意这似乎对限制太严格了(如果所有成员都可以变异怎么办?如果您想阅读单个成员的默认成员初始化器,而其他成员会使其他成员启用?如果您想构建虚拟对象怎么办?不过,要把所有东西都传递给其他所有东西吗? const学生&amp; S = _s; 基本上具有相同的效果。


这是编译器提供的默认构造函数和显式Student(){}之间的区别之一。还有一些是:

  • 用户提供一个不是琐碎的,也不是聚集的(并且在恒定表达式中不可用)
  • noexcept ness-自动确定
  • 编译器,只要一个人可以是<代码>删除 d
  • 用户提供的构造函数明确地将实例化集体正文中成员/碱基的所有默认构造函数/成员初始化器(而不是在尝试使用时)

,甚至可能更多。这些是用户提供的和编译器生成的构造函数之间的差异,这是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; and int 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:

A const object cannot be assigned to so it must be initialized to hold a useful value.

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:

  • The user provided one isn't trivial nor an aggregate (and this one wouldn't be usable in a constant expression)
  • noexcept-ness is automatically determined
  • The compiler provided one may be deleted
  • The user provided constructor explicitly will instantiate all the default constructors/member initializers of the members/bases in the body of the class (rather than when you attempt to use it)

And 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文