在哪些情况下根本没有构造函数,甚至没有默认构造函数? [英] In which cases there is no constructor at all, even a default constructor?

查看:78
本文介绍了在哪些情况下根本没有构造函数,甚至没有默认构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这本书中,我目前正在阅读我遇到的这本书:

In this book I am currently reading I ran across this:

一个类不需要构造函数.如果对象不需要初始化,则不需要默认的构造函数.

A class doesn't need a constructor. A default constructor is not needed if the object doesn't need initialization.

从上述推断,我是否正确,在某些情况下编译器不会为类/结构生成默认构造函数?如果是,那是什么情况?我敢说POD可能就是其中之一.还有其他吗?

Am I correct in inferring from the above that the compiler does not generate a default constructor for the class/structure in some cases? If yes, what are those cases? I will venture and say POD is probably one. Are there any other?

我已经更改了标题,因为原始标题的含义是我询问何时未定义默认构造函数,而不是询问何时类根本没有构造函数.

I have changed the title as the original title gave the meaning that I asked when was a default constructor not defined instead of asking when does a class not have a constructor at all.

推荐答案

一个类不需要构造函数.如果对象不需要初始化,则不需要默认的构造函数.

A class doesn't need a constructor. A default constructor is not needed if the object doesn't need initialization.

我认为作者正在谈论这种情况:

I think the author is talking about this situation:

some_type some_function () {
   POD_type this_is_intentionally_uninitialized;
   ...
}

在某些情况下,不会调用构造函数,句号.一旦您编写了构造函数,便没有POD类,因此现在将调用该构造函数.

Under some circumstances a constructor won't be called, period. As soon as you write a constructor you don't have a POD class, so now the constructor will be called.

让一个包含随机未初始化数据的对象运行是好事还是坏事,完全是另一个问题.

Whether it is a good or bad thing to have an object running around that contains random, uninitialized data is a different question entirely.

这篇关于在哪些情况下根本没有构造函数,甚至没有默认构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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