返回 NULL 值

发布于 2024-10-31 17:05:53 字数 704 浏览 1 评论 0原文

Framebufferd3d11.h 的片段

namespace dx11 {
...
class FramebufferManager : public FramebufferManagerBase
{
public:
   ...
private:
   ...
   static struct Efb
   {
      ...
      std::unique_ptr<D3DTexture2D> resolved_color_tex;
      std::unique_ptr<D3DTexture2D> resolved_depth_tex;
   } m_efb;
};
} //namespace

Framebufferd3d11.cpp 的片段

namespace DX11
{
...
FramebufferManager::Efb FramebufferManager::m_efb;
...
FramebufferManager::FramebufferManager()
{
   ...
   m_efb.resolved_color_tex = NULL;
   m_efb.resolved_depth_tex = NULL;
}
} //namespace

如果我使用 icc 进行编译,则会遇到 NULL 赋值问题,因为 NULL 被定义为 0。我该如何解决这样的问题?

Snippets of framebufferd3d11.h

namespace dx11 {
...
class FramebufferManager : public FramebufferManagerBase
{
public:
   ...
private:
   ...
   static struct Efb
   {
      ...
      std::unique_ptr<D3DTexture2D> resolved_color_tex;
      std::unique_ptr<D3DTexture2D> resolved_depth_tex;
   } m_efb;
};
} //namespace

Snippets of framebufferd3d11.cpp

namespace DX11
{
...
FramebufferManager::Efb FramebufferManager::m_efb;
...
FramebufferManager::FramebufferManager()
{
   ...
   m_efb.resolved_color_tex = NULL;
   m_efb.resolved_depth_tex = NULL;
}
} //namespace

If I compile with icc, I get a problem with NULL assign value, as NULL is defined as 0. How do I solve a problem like this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

最终幸福 2024-11-07 17:05:53

你的代码是正确的。以下所有内容都应该有效:

m_efb.resolved_color_tex = 0;
m_efb.resolved_color_tex = NULL;
m_efb.resolved_color_tex = nullptr;

空指针常量(如 0NULL)可隐式转换为 nullptr_tunique_ptr 有一个采用 nullptr_t 的赋值运算符。如果您使用的 ICC 版本尚不支持 nullptr,这可以解释为什么 NULL 的分配对您不起作用。

使用 reset() 可能会有更好的运气:

m_efb.resolved_color_tex.reset(NULL);

或者,因为 NULL 是默认参数:

m_efb.resolved_color_tex.reset(); 

Your code is correct. All of the following should work:

m_efb.resolved_color_tex = 0;
m_efb.resolved_color_tex = NULL;
m_efb.resolved_color_tex = nullptr;

A null pointer constant (like 0 or NULL) is implicitly convertible to nullptr_t and unique_ptr has an assignment operator that takes a nullptr_t. If the version of ICC that you are using does not yet support nullptr, that could explain why the assignment of NULL doesn't work for you.

You may have better luck using reset():

m_efb.resolved_color_tex.reset(NULL);

or, since NULL is the default argument:

m_efb.resolved_color_tex.reset(); 
梦里人 2024-11-07 17:05:53

切勿在构造函数主体中初始化成员。如果它们具有重要的构造函数,则首先调用它,然后使用 operator= 重新初始化它们。而是使用基类和成员初始值设定项列表。就像:

FramebufferManager::FramebufferManager() :
   resolved_color_tex(NULL), resolved_depth_tex(NULL)
{
   ...
}

在你的情况下,事实证明:

  1. 默认构造函数(如果你提到 resolved_color_tex() 或根本没有提到它,就会调用它)已经做了你想要的事情。
  2. 对于原始类型,如果您明确提及它们,它们将被零初始化。因此,任何指针成员,无论什么类型,如果在初始化列表中明确提及,都将为 NULL,您不必再考虑它。

Never initialize members in the body of the constructor. If they have non-trivial constructor, it is first called and than operator= is used to reinitialize them. Instead use the base and member initializer list. Like:

FramebufferManager::FramebufferManager() :
   resolved_color_tex(NULL), resolved_depth_tex(NULL)
{
   ...
}

In your case it turns out that:

  1. The default constructor (called if you mention either resolved_color_tex() or not mention it at all) already does what you want.
  2. For primitive types, if you explicitly mention them, they will be zero-initialized. So any pointer members, no matter what type, if explicitly mentioned in the initializer list, will be NULL and you don't have to think about it any more.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文