Default constructor/destructor outside the class?

自古美人都是妖i 提交于 2019-12-21 03:37:34

问题


Is the following legal according to the C++11 standard (= default outside the definition of the class) ?

// In header file
class Test
{
    public:
        Test();
        ~Test();
};

// In cpp file
Test::Test() = default;
Test::~Test() = default;

回答1:


Yes, a special member function can be default-defined out-of-line in a .cpp file. Realize that by doing so, some of the properties of an inline-defaulted function will not apply to your class. For example, if your copy constructor is default-defined out-of-line, your class will not be considered trivially copyable (which also disqualifies it from being recognized as a POD). Similarly, a default-defined out-of-line destructor will disqualify your type from being trivial (or POD).

This can be useful if you wish to have a non-inline copy-constructor and control over where it is defined (perhaps to take control over generated template definitions it will require), but don't wish to manually define it yourself with a hand-crafted member-initializer list, which would be laborious and could go stale under maintenance.



来源:https://stackoverflow.com/questions/14256726/default-constructor-destructor-outside-the-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!