Nullptr并检查指针是否指向有效对象 [英] Nullptr and checking if a pointer points to a valid object

查看:135
本文介绍了Nullptr并检查指针是否指向有效对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的几个老代码项目,当我从来没有听说过智能指针,每当我需要检查指针是否仍指向一个有效的对象,我会总是做这样的...

In a couple of my older code projects when I had never heard of smart pointers, whenever I needed to check whether the pointer still pointed to a valid object, I would always do something like this...

object * meh = new object;
if(meh) 
    meh->member;

或者当我需要安全删除对象时,像这样

Or when I needed to delete the object safely, something like this

if(meh)
{
    delete meh;
    meh = 0;
}

现在我已经了解了使用对象会产生的问题,布尔表达式中的指针都有字面数字,硬的方式:。现在我还学习了C ++的新特性,但是非常酷的特性, nullptr 关键字。但现在我很好奇。

Well, now I have learned about the problems that can arise from using objects and pointers in boolean expressions both with literal numbers, the hard way :. And now I've also learned of the not so new but pretty cool feature of C++, the nullptr keyword. But now I'm curious.

我已经经历并修改了我的大部分代码,例如,当我删除对象时,我现在写

I've already gone through and revised most of my code so that, for example, when deleting objects I now write

if(meh)
{
    delete meh;
    meh = nullptr;
}

现在我想知道布尔值。当你传递一个int到这样的if语句时,

Now I'm wondering about the boolean. When you pass just say an int into an if statement like this,

int meh;
if(meh)

然后它隐式检查零而不需要写。

Then it implicitly checks for zero without you needing to write it.

if(meh == 0) // does the exact same check

现在,C ++对指针做同样的事吗?如果将这样的char *传递给if语句?

Now, will C++ do the same for pointers? If pass in a char * like this to an if statement?

char * meh;
if(meh)

那么它会隐式地与nullptr比较吗?因为我写了这么多ifs像这样,这是第二个性质,在这一点上检查指针是否在使用前通过键入if(object *),然后调用其成员有效。如果这不是功能为什么不呢?太难实现了?

Then will it implicitly compare it with nullptr? Because of how long I have been writing these ifs like this, it is second nature at this point to check if the pointers valid before using by typing if (object *) and then calling its members. If this is not the functionality why not? Too difficult to implement? Would solve some problems by removing yet another tiny way you could mess up your code.

推荐答案

在C中,不是0的任何东西都是真正。所以,你肯定可以使用:

In C, anything that's not 0 is true. So, you certainly can use:

if (ptrToObject) 
    ptrToObject->doSomething();

以安全地取消引用指针。

to safely dereference pointers.

C ++ 11改变游戏有点, nullptr_t nullptr 是一个实例的类型; nullptr_t 的表示是实现特定的。因此,编译器可以定义 nullptr_t ,但它想要。它只需要确保它可以强制对 nullptr_t 对不同类型(其中允许使用 boolean )进行适当的限制 - 确定它可以区分 nullptr_t 和0。

C++11 changes the game a bit, nullptr_t is a type of which nullptr is an instance; the representation of nullptr_t is implementation specific. So a compiler may define nullptr_t however it wants. It need only make sure it can enforce proper restriction on the casting of a nullptr_t to different types--of which boolean is allowed--and make sure it can distinguish between a nullptr_t and 0.

因此 nullptr 将适当地并隐式地转换为 boolean false

So nullptr will be properly and implicitly cast to the boolean false so long as the compiler follows the C++11 language specification. And the above snippet still works.

如果您删除一个引用对象,则没有任何变化。

If you delete a referenced object, nothing changes.

delete ptrToObject;
assert(ptrToObject);
ptrToObject = nullptr;
assert(!ptrToObject);








long我一直在写这些ifs像这样,这是第二个性质在这一点,通过键入if(object *)然后调用它的成员来检查指针是否有效在使用之前。

Because of how long I have been writing these ifs like this, it is second nature at this point to check if the pointers valid before using by typing if (object *) and then calling it's members.

否。 请保持对象的正确图形(最好使用唯一/智能指针)。正如所指出的,没有办法确定一个不是 nullptr 的指针是否指向一个有效的对象。这是为什么指针包装器存在于第一位。

No. Please maintain a proper graph of objects (preferably using unique/smart pointers). As pointed out, there's no way to determine if a pointer that is not nullptr points to a valid object or not. The onus is on you to maintain the lifecycle anyway.. this is why the pointer wrappers exist in the first place.

这篇关于Nullptr并检查指针是否指向有效对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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