Unique_ptr and forward declaration

后端 未结 2 922
礼貌的吻别
礼貌的吻别 2020-11-30 11:28

Say I have two classes:

\"Foo.h\"

#pragma once    
class Foo
{
public:
    Foo()
    {

    };

    ~Foo()
    {

    };
};

\"A.h\"

相关标签:
2条回答
  • 2020-11-30 11:47

    The constructor needs access to the deleter in just the same way the destructor does: exception safety requires that the constructor be able to roll-back initialisation of all the members in the case that your constructor's body throws:

    [C++14: 12.6.2/10]: In a non-delegating constructor, the destructor for each potentially constructed subobject of class type is potentially invoked (12.4). [ Note: This provision ensures that destructors can be called for fully-constructed sub-objects in case an exception is thrown (15.2). —end note ]

    Related:

    • https://stackoverflow.com/a/26995534/560648
    0 讨论(0)
  • 2020-11-30 12:08

    It is no possible for 'A' to not have a constructor.

    If you comment the constructor you wrote, the compiler will make a default constructor for you and it won't necessarily be in the same place you defined the one you made. Causing said problem.

    0 讨论(0)
提交回复
热议问题