Is std::unique_ptr required to know the full definition of T?

前端 未结 9 1601
小鲜肉
小鲜肉 2020-11-22 07:47

I have some code in a header that looks like this:

#include 

class Thing;

class MyClass
{
    std::unique_ptr< Thing > my_thing;
};
         


        
9条回答
  •  我寻月下人不归
    2020-11-22 07:59

    I was looking for a way to use the PIMPL idiom with std::unique_ptr. This guide is a great resource.

    In short, here's what you can do to make it work:

    my_class.h

    #include 
    
    class Thing;
    
    class MyClass
    {
        ~MyClass(); // <--- Added
        std::unique_ptr< Thing > my_thing;
    };
    

    my_class.cpp

    MyClass::~MyClass() = default; // Or a custom implementation
    

提交回复
热议问题