在C ++中自初始化为nullptr的简单原始指针 [英] Trivial raw pointer that self-initializes to nullptr in C++

查看:493
本文介绍了在C ++中自初始化为nullptr的简单原始指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢C ++ 11中的新指针类型,但有时我仍然需要一个原始指针。但是,使得我对C ++中的原​​始类型更加悲伤的东西是他们在没有给出明确的值的时候初始化为未定义的习惯。由于我更常使用std :: shared_ptr<>等,这需要初始化裸指针到null感觉越来越脆弱和不必要。我在说:

  class foo 
{
...
std: :shared_ptr< bar> pb; //最初在任何构造函数中为null。
std :: unique_ptr< dar> pd; //同样。
std :: weak_ptr< gar> pg; // 然后再次。
lar * pr; //噢哦!谁知道这是什么?更好记得初始化...
};

foo :: foo(int j)
:pr(nullptr)
{...}

foo :: foo(const string& s)
:pr(nullptr)
{...}

...等:很多乏味和容易出错的构造函数定义如下。

因此,我想要的是一个null初始化的原始指针。类似的东西:

  class foo 
{
...
std :: shared_ptr< bar> pb; //最初在任何构造函数中为null。
std :: unique_ptr< dar> pd; //同样。
std :: weak_ptr< gar> pg; // 然后再次。
raw_ptr< lar> pr; //再一次感觉。
};

foo :: foo(int j)
{...} //不需要显式指针初始化。

foo :: foo(const string& s)
{...}

...

更准确地说,我想要的是一个简单的,廉价的类型,其行为就像一个裸指针,除了它的默认构造函数初始化为nullptr。 / p>

我的问题:(1)这样的事情已经存在于标准库中了吗? (2)如果没有,什么是最优雅/最小的方法来完成这种类型的实现?



我对Boost或任何其他库不感兴趣,除非它是一个单一文件中的头文件库。

解决方案

C ++ 11允许在数据成员的类初始化中。

  class foo 
{
// ...
lar * pr = nullptr;
};

这将始终初始化 pr nullptr ,除非在构造函数中指定另一个值。


I like the new pointer types in C++11, but sometimes I still need a raw pointer. Something that makes me increasingly sad about "raw" types in C++, however, is their habit of initializing as undefined when not given an explicit value. As I use std::shared_ptr<> and the like more often, this need to initialize raw pointers to null feels increasingly brittle and unnecessary. I'm talking about:

class foo
{
    ...
    std::shared_ptr< bar > pb;   // Initially null in whatever constructor.
    std::unique_ptr< dar > pd;   // Likewise.
    std::weak_ptr< gar > pg;     // And again.
    lar* pr;                     // Uh-oh! Who knows what this is? Better remember to initialize...
};

foo::foo( int j )
: pr( nullptr )
{...}

foo::foo( const string& s )
: pr( nullptr )
{...}

... etc.: many tedious and error-prone constructor definitions follow.

What I'd like, therefore, is a "raw pointer with null initialization." Something like:

class foo
{
    ...
    std::shared_ptr< bar > pb;   // Initially null in whatever constructor.
    std::unique_ptr< dar > pd;   // Likewise.
    std::weak_ptr< gar > pg;     // And again.
    raw_ptr< lar > pr;           // Once more with feeling.
};

foo::foo( int j )
{...}                            // No explicit pointer initialization necessary.

foo::foo( const string& s )
{...}

...

More precisely, what I want is a simple, cheap type that acts exactly like a raw pointer in every way except that its default constructor initializes it to nullptr.

My question: (1) Does such a thing already exist in the standard library? (2) If not, what would be the most elegant/smallest way to accomplish an implementation of this type?

P.S. I'm not interested in Boost or any other libraries, unless perhaps it is a header-only library in a single file. Smallness and simplicity are of the essence.

解决方案

C++11 allows in class initialization of data members.

class foo
{
  // ...
  lar *pr = nullptr;
};

That'll always initialize pr to nullptr unless you assign another value in the constructor.

这篇关于在C ++中自初始化为nullptr的简单原始指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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