当使用常量*或常量* const的? [英] When to use const* versus const * const?

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

问题描述

我见过code,人们会用常量作为参数传递给函数。什么是使用常量* VS常量* const的好处?这可能是一个很基本的问题,但我会AP preciate如果有人可以解释的。

 布尔IsThisNumberEqualToFive(INT常量* NUM)
{
    返回(布尔)(5 = NUM​​!);
}布尔IsThisNumberEqualToFive(INT常量* const的NUM)
{
    返回(布尔)(5 = NUM​​!);
}


解决方案

  • 在第一个版本,你答应你不会写入
    反对说 NUM

  • 在第二个版本中,你承诺,你也$ P $自己pvent
    (即IsThisNumber ..)从制作 NUM 指向别的东西。

也就是说,在第二个版本的指针本身也是常量 ,除了指针对象。

I have seen code where people will use const as a parameter to the function. What are the benefits of using const* vs const * const? This might be a very basic question but I would appreciate if someone could explain.

Bool IsThisNumberEqualToFive(int const * num)
{
    return(Bool)(5 != num );
}

Bool IsThisNumberEqualToFive(int const * const num)
{
    return(Bool)(5 != num );
}

解决方案

  • In the first version you promise you're not going to write to the object that num points to
  • In the second version you promise that and you also prevent yourself (i.e. IsThisNumber..) from making num point to something else.

That is, in the second version the pointer itself is also const, in addition to the pointee.

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

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