gpt4 book ai didi

c - 什么时候使用 const* vs const * const?

转载 作者:行者123 更新时间:2023-12-01 08:23:26 27 4
gpt4 key购买 nike

我见过人们将 const 用作函数参数的代码。使用 const* 与 const * const 有什么好处?这可能是一个非常基本的问题,但如果有人能解释一下,我将不胜感激。

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

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

最佳答案

  • 在第一个版本中你保证你不会写信给num 指向的对象
  • 在第二个版本中,你 promise 了这一点,同时你也阻止了自己(即 IsThisNumber..)使 num 指向其他东西。

也就是说,在第二个版本中除了指针对象之外,指针本身也是const

关于c - 什么时候使用 const* vs const * const?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21097690/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com