什么时候应该使用C ++中的引用? [英] When should I use references in C++?

查看:353
本文介绍了什么时候应该使用C ++中的引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在编写C ++一段时间,我开始怀疑规则尽可能使用引用应该应用到任何地方。



此相关SO职位不同,我有兴趣不同类型的东西。



在我的经验中,引用/指针混合混淆你的代码:

  std :: vector< Foo *> & x = get_from_somewhere(); // 好?引用作为返回值
some_func_pass_by_ref(x); // OK引用参数和引用变量
some_func_by_pointer(x [4]); // OK指针arg,指针值
some_func_pass_elem_by_ref(* x [5]); // BAD:pointer value,reference argument
some_func_that_requires_vec_ptr(& x); // BAD:reference value,pointer argument

一个选项是替换 & with * const like Foo& with Foo * const

  void some_func_by_ref(const std :: vector< Foo * const> * const); // BAD:verbose! 

这种方式至少遍历不见了。和我重写函数头没有了,因为所有的参数将是指针...以污染代码的代价与 const 而不是指针算术(主要& * )。





考虑:

时,


  • 最小的重写函数原型(即:哦,我需要重写一堆原型,因为我想把这个引用的元素放入容器) li>
  • 提高可读性




    • 避免应用 * Foo * 转换为 Foo& ,反之亦然。

    • 避免过多的const使用,如 * const



b $ b

注意:我想到的一件事是使用指针,每当我打算把元素放入 STL 容器(参见boost :: ref)



我不认为这是C ++ 03具体但是C ++ 11解决方案是可以的,如果他们可以反向到C ++ 03(即:NRVO而不是移动语义)。

解决方案

以下方法似乎是合理的:




  • boost和C ++ 11有一个类可以方便地用于在容器中存储引用:参考包装器

  • 一个好的建议是使用句柄/身体成语而不是传递原始指针。这也解决了由引用或指针管理的内存的所有权问题。来自Adobe的 Sean Parent 已经在

    我选择使用Handle / Body Idiom方法,因为它给了指针自动复制/分配行为同时隐藏底层实现和所有权语义。它也作为一种编译时防火墙减少头文件包含。


    I've been programming C++ for a while now and I'm starting to doubt that the rule use references whenever possible should be applied everywhere.

    Unlike this related SO post I'm interested in a different kind of thing.

    In my experience the reference/pointer mix messes up your code:

    std::vector<Foo *> &x = get_from_somewhere(); // OK? reference as return value
    some_func_pass_by_ref(x); // OK reference argument and reference variable
    some_func_by_pointer(x[4]); // OK pointer arg, pointer value
    some_func_pass_elem_by_ref(*x[5]); // BAD: pointer value, reference argument
    some_func_that_requires_vec_ptr(&x); // BAD: reference value, pointer argument
    

    One option would be to replace & with * const like Foo & with Foo * const

    void some_func_by_ref(const std::vector<Foo * const> * const); // BAD: verbose!
    

    this way at least the traversals are gone. and me rewriting function headers is gone, because all arguments will be pointers... at the price of polluting the code with const instead of pointer arithmetic (mainly & and *).

    I would like to know how and when you apply use references whenever possible rule.

    considering:

    • minimal rewriting of function prototypes (i.e.: oh damn I need need to rewrite alot of prototypes because I want to put this referenced element into a container)
    • increasing readability

      • avoid application of * to transform Foo* to Foo& and vice versa
      • avoid excessive const usage as in * const

    NOTES: one thing I figured is to use pointers whenever I intend to ever put the element into an STL container (see boost::ref)

    I don't think this is something C++03 specific but C++11 solutions are fine if they can be backported to C++03 (i.e.: NRVO instead of move-semantics).

    解决方案

    The following ways seem reasonable dealing with this:

    • boost and C++11 have a class that can cheaply be used to store references in a container: Reference Wrapper
    • A good advice is to use the handle/body idiom more often instead of passing around raw pointers. This also solves the ownership issue of the memory that is governed by the reference or the pointer. Sean Parent from Adobe has pointed this out at a talk at going native 2013.

    I chose to use the Handle/Body Idiom approach because it gives pointers automatically copy/assign behaviour while hiding the underlying implementation and ownership semantics. It also acts as kind of a compile time firewall reducing header file inclusion.

    这篇关于什么时候应该使用C ++中的引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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