指针与引用返回类型

发布于 2024-12-10 15:25:16 字数 522 浏览 1 评论 0原文

我试图提出一个具体的推理为什么要使用指针而不是引用 作为函数的返回类型,

我的推理是,如果无意中将空值返回给引用类型 它无法被检查,并且可能导致运行时错误,

  int& something(int j)
  {
      int* p = 0 ;  
      return *p;
  }

  void main()
  {
      int& i = something(5);
      i = 7;   // run-time error 
  }

如果我使用了指针,我可以检查它并避免错误 指针返回值将充当必须返回值的契约。

 void main()
 {
         int* i = something(5);
         if( i != null )
             *i = 7;
 }

任何想法将不胜感激 再说一次,

你会用什么以及为什么 参考或指针。

提前感谢

i'm trying to come up with a concrete reasoning why to use a pointer over a reference
as a return type from a function ,

my reasoning is that if inadvertently a null value is returned to a reference type
it could not be checked , and could lead to run-time errors

  int& something(int j)
  {
      int* p = 0 ;  
      return *p;
  }

  void main()
  {
      int& i = something(5);
      i = 7;   // run-time error 
  }

if i had used a pointer i could check it and avoid the error
the pointer return value would act as a contract to that a value must be returned.

 void main()
 {
         int* i = something(5);
         if( i != null )
             *i = 7;
 }

any thoughts would be appreciated
again ,

what would you use and why
reference or pointer

thanks in advance.

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

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

发布评论

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

评论(6

私藏温柔 2024-12-17 15:25:16

如果满足以下条件,则可以使用指针而不是引用:

  • Null 是有效的返回值
  • 您在函数中动态构造了某些内容,并且接收者成为所有者。 (在这种情况下,您可能会考虑返回一个智能指针,例如 std::unique_ptr 或 boost::shared_ptr。)

无论如何,您都不想返回对局部变量的指针或引用。

You could use a pointer instead of a reference if:

  • Null is a valid return value
  • You dynamically constructed something in the function, and the recipient becomes the owner. (In this case, you might consider returning a smart pointer such as std::unique_ptr or boost::shared_ptr.)

Regardless, you would not want to return either a pointer or a reference to a local variable.

迷离° 2024-12-17 15:25:16

参考文献是一种不同的思维方式。将引用视为“指向现有对象的指针”。一旦你这样做了,你就会明白为什么它们不能为 NULL - 对象存在并且引用指向它。

因此,如果您的函数返回对其创建的对象的引用,则它需要保证它确实创建了一个有效的对象。如果没有,或者不能,那么就有理由抛出异常。

将其与指针进行对比。指针可以为 NULL,并且调用者必须处理 NULL 返回值。因此,如果您的函数不能保证它将返回有效的引用并且您不想引发异常,则需要使用指针。

References are a different way of thinking. Think of references as "pointers to existing objects". Once you do that, you'll understand why they can't be NULL - the object exists and the reference points to it.

Therefore, if your function returns a reference to something that it creates, it needs to guarantee that it actually does create a valid object. If it does not, or is unable to, then that is grounds to throw an exception.

Contrast that with a pointer. A pointer can be NULL and the caller will have to deal with a NULL return value. Therefore, if your function cannot guarantee that it will return a valid reference and you don't want to throw exceptions, you will need to use pointers.

水波映月 2024-12-17 15:25:16

如果你无意中返回了一个空值,那就是一个错误。您可以轻松地将检查放在 something() 中,并在其为 null 时抛出异常。

话虽如此,历史惯例是通过指针返回堆对象,即使它们保证为非空。

If you inadvertently return a null value, that's a bug. You can just as easily place the check inside something() and throw an exception if it's null.

Having said that, the historical convention is to return heap objects via pointers, even if they are guaranteed to be non-null.

桜花祭 2024-12-17 15:25:16

如果您的函数旨在“始终”返回一个值,那么您的函数应该返回一个引用。

在某些情况下,由于某种特殊原因,您无法找到要返回的值,您应该抛出异常。

当您尝试返回对空指针或野指针的引用时,不应依赖于生成运行时错误。该行为是未定义的。什么事情都可能发生。

If your function is intended to "always" return a value, then your function should return a reference.

In those cases where, for some exceptional reason, you cannot find the value to return, you should throw an exception.

You should not rely on there being a run-time error generated when you try to return a reference to a null or wild pointer. The behavior is undefined. Anything could happen.

聚集的泪 2024-12-17 15:25:16

C++ 引用不能为空。该错误是取消引用空指针。这是未定义的行为。

C++ references cannot be null. The bug is dereferencing a null pointer. That's undefined behaviour.

倒数 2024-12-17 15:25:16

它通常取决于您想要完成的任务,但我通常这样处理返回值:

返回指针 - 调用者拥有内存(和清理)

引用 - 被调用者拥有内存。不要返回动态分配的点,除非被调用者也管理它。

It is usually dependent on what you are trying to accomplish, but I generally treat return values this way:

Return pointer - Caller owns memory (and cleanup)

Reference - Callee owns the memory. DO NOT return a dynamically allocated point unless the callee manages it as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文