pointer vs non-pointer members of a class

前端 未结 5 1068
深忆病人
深忆病人 2021-02-02 02:26

My questions is, suppose we have two classes A and B. I want to have an object of B in class A.

Should I use,

class A
{
  public:
          A();
                 


        
5条回答
  •  醉梦人生
    2021-02-02 02:49

    If you want an object of class B in A, then you must use the second. The first doesn't give you an object of type B in A, it gives you an object of type B* in A.

    The second is also more convenient for almost all purposes. Only use a pointer when you have to, and even then resource-handling is more convenient with a smart pointer rather than a B*.

提交回复
热议问题