zoukankan      html  css  js  c++  java
  • 像Java一样管理对象:T&形式仅仅用在参数传递

    类的对象为了关联/包含一个T类型的instance,若成员变量包括T*/ T&, 这种设计叫做“aggregation”(聚合);而若采用T 形式,则称为"composition"(组合)

     1 //组合Composition
     2 class Man {
     3     Eye eye;
     4     Nose nose;
     5 }
     6 
     7 //聚合Aggregation
     8 class Man {
     9     Dog* dog;
    10     House& house;
    11 }

    这个回答不错,百度知道:☛ 组合和聚合的区别?


    怎样看待“引用类型作为类的成员变量”?

    参考StackOverflow上此问题的回答:☛ Reference member variables as class members

     尤其是其中 manlio的回答

    It's called dependency injection via constructor injection: (通过构造函数进行依赖注入)

    class A gets the dependency as an argument to its constructor and saves the reference to dependent class as a private variable.

    For const-correctness I'd write:

    using T = int;
    
    class A
    {
    public:
      A(const T &thing) : m_thing(thing) {}
      // ...
    
    private:
       const T & m_thing;
    };
    

    but a problem with this class is that it accepts references to temporary objects:

    T t;
    A a1{t};    // this is ok, but...
    
    A a2{T()};  // ... this is BAD.    //临时的匿名对象 属于 rvalue
    

    It's better to add (requires C++11 at least):

    class A
    {
    public:
      A(const T &thing) : m_thing(thing) {}
      A(const T &&) = delete;  // prevents rvalue binding
      // ...
    
    private:
      const T &m_thing;
    };
    

    Anyway if you change the constructor:

    class A
    {
    public:
      A(const T *thing) : m_thing(*thing) { assert(thing); }
      // ...
    
    private:
       const T &m_thing;
    };
    

    it's pretty much guaranteed that you won't have a pointer to a temporary.

    Also, since the constructor takes a pointer, it's clearer to users of A that they need to pay attention to the lifetime of the object they pass.


    使用T&作为成员变量后:

    ①各个Contructor里必须对此T& t进行赋值。

    ②对象生成后就不能再对它进行赋值(=),因为引用不能二次赋值。

    在此提问  ☛Should I prefer pointers or references in member data?  下, anon的回答:

    As everyone seems to be handing out general rules, I'll offer two:

    • Never, ever use references as class members. I have never done so in my own code (except to prove to myself that I was right in this rule) and cannot imagine a case where I would do so. The semantics are too confusing, and it's really not what references were designed for. (引用& 最初就是为了 运算符重载时好看 而设计出来的)

    • Always, always, use references when passing parameters to functions, except for the basic types, or when the algorithm requires a copy.

    These rules are simple, and have stood me in good stead. I leave making rules on using smart pointers (but please, not auto_ptr) as class members to others.

    即:▶T& 形式仅仅用在 参数传递       ▶作为成员变量都用T* 形式 (绝不要用T&)

  • 相关阅读:
    前端错误监控
    MeasureSpec内容
    java异常
    JAVA线程的xiao习:线程池 线程同步 线程间通讯 =》进程间通信
    四大组件的context的研究
    初遇Java StringBuffer 和 StringBuilder 类&&利用 StringBuilder 给TextView实现换行处理
    AlertDialog 的context 不能是application的context
    BroadCast用法以及notification用法
    安卓 ContentProvider
    远程service的使用思路
  • 原文地址:https://www.cnblogs.com/nanlan2017/p/9193477.html
Copyright © 2011-2022 走看看