何时使用Q_NULLPTR? [英] When to use Q_NULLPTR?

查看:386
本文介绍了何时使用Q_NULLPTR?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到在Qt源代码和示例中广泛使用了 Q_NULLPTR ,但是我没有找到确切的名称以及何时使用它的文档。



例如在此官方演示中在新Qt v5.6中添加的新Qt SerialBus模块:

  if(!m_canDevice-> connectDevice()){
delete m_canDevice;
m_canDevice = Q_NULLPTR;

此服务是否用于 nullptr 的目的到在C ++ 11中添加的内容?如果是这样,现在我们有了C ++ 11,是否应该使用 Q_NULLPTR



PS:我尝试过在Qt源代码中搜索宏的定义,但找不到。

解决方案


Did这在将nullptr添加到
C ++ 11中之前达到了目的吗?如果是这样,那么现在我们有了C ++ 11,我应该使用Q_NULLPTR吗?


分别是(有点)和否。 / p>

过去,C ++相当缺乏,因此Qt拥有自己的东西,后来随着C ++赶上这些功能而变得过时。



话虽如此, Q_NULLPTR 在功能上与 nullptr 不同,(正如Andrei指出的那样,如果支持C ++ 11,它会扩展为 nullptr ),但它没有为您提供类型安全性,只是语法糖。它说明了阅读代码的人的意图,而不是像 nullptr 那样对编译器的意图。


I see Q_NULLPTR being used liberally in Qt source code and examples, but I have found no documentation for what it is exactly and when it should be used.

For example in this official demonstration of the new Qt SerialBus module added in the new Qt v5.6:

if (!m_canDevice->connectDevice()) {
    delete m_canDevice;
    m_canDevice = Q_NULLPTR;

Did this serve the purpose of nullptr prior to that being added in C++11? If so, now that we have C++11, should I be using Q_NULLPTR?

PS: I tried searching the Qt source code for the definition of the macro but failed to find it.

解决方案

Did this serve the purpose of nullptr prior to that being added in C++11? If so, now that we have C++11, should I be using Q_NULLPTR?

Yes (somewhat) and No respectively.

C++ was quite lacking back in the days, so Qt had its own stuff, which later became obsolete as C++ caught up on the features.

That being said, Q_NULLPTR is (was) not functionally the same as nullptr, (as Andrei noted, if C++11 is supported it expands to nullptr) it didn't give you the type safety, just syntax "sugar". It illustrated the intent to the person reading the code, not to the compiler as nullptr does.

这篇关于何时使用Q_NULLPTR?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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