pointer vs non-pointer members of a class

前端 未结 5 1081
深忆病人
深忆病人 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:39

    In this scenario, the non-pointer version is more convenient. You don't need to worry about invoking delete at the correct time, and therefore you don't need to worry about destructors, copy constructors and copy assignment operators.

    In general, you should avoid dynamically-allocated memory as much as possible.

提交回复
热议问题