gpt4 book ai didi

c++ - Unique_ptr 和前向声明

转载 作者:行者123 更新时间:2023-12-01 14:57:04 25 4
gpt4 key购买 nike

假设我有两个类(class):

“Foo.h”

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

};

~Foo()
{

};
};

“啊”
#pragma once
#include <memory>

class Foo;

class A
{
public:
A(){};
~A(){};

std::unique_ptr<Foo> foo;
};

A持有 unique_ptrFoo .我不想包含 Foo在“A.h”中,所以我转发了它。通过向前声明类 Foo在“A.h”中,我得到一个编译时错误:
error C2027: use of undefined type 'Foo'
error C2338: can't delete an incomplete type

所以我关注了 this关于如何避免此错误并将 A 的析构函数移到它自己的 .cpp 文件中的文章,我还包括 Foo:

“A.cpp”
#include "A.h"

#include "Foo.h"

A::A()
{

}

A::~A()
{

}

在“A.cpp”中实现 A 的析构函数后,我可以编译程序,因为 Foo 类在“A.cpp”中是已知的。这似乎合乎逻辑,因为 unique_ptr 需要完整的类型才能将其称为析构函数。但令我惊讶的是,在注释掉 A 的构造函数(在“A.h”和“A.cpp”中)之后,我得到了同样的错误。这怎么可能?为什么编译器会提示 A 没有构造函数时无法调用 Foo 的析构函数?

编辑:
我上传了 4 个文件,以便您可以测试程序。
我正在使用 Visual Studio 2013 的 MSVC++。

http://www.filedropper.com/test_61

最佳答案

构造函数需要以与析构函数相同的方式访问删除器:异常安全要求构造函数能够在构造函数的主体抛出的情况下回滚所有成员的初始化:

[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 ]



有关的:
  • https://stackoverflow.com/a/26995534/560648
  • 关于c++ - Unique_ptr 和前向声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63650250/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com