When to use shared_ptr and when to use raw pointers?

后端 未结 10 1370
情歌与酒
情歌与酒 2020-11-29 16:27
class B;

class A
{
public:
    A ()
        : m_b(new B())
    {
    }

    shared_ptr GimmeB ()
    {
        return m_b;
    }

private:
    shared_ptr&l         


        
10条回答
  •  温柔的废话
    2020-11-29 17:23

    1. You allocate B at constuction of A.
    2. You say B shouldn't persist outside As lifetime.
      Both these point to B being a member of A and a just returning a reference accessor. Are you overengineering this?

提交回复
热议问题