jav*_*ver 5 c++ pimpl-idiom unique-ptr incomplete-type c++11
std::unique<B>我为incomplete type创建了一个小测试用例B。
测试.h
#pragma once
#include <memory>
class B; //<--- compile error here
class Test{
std::unique_ptr<B> bPtr;
//#1 need to move destructor's implementation to .cpp
public: ~Test();
};
Run Code Online (Sandbox Code Playgroud)
测试.cpp
#include "Test.h"
class B{};
Test::~Test(){} //move here because it need complete type of B
Run Code Online (Sandbox Code Playgroud)
主程序
#include <iostream>
#include "Test.h"
using namespace std;
int main(){
Test test;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我收到此错误:-
/usr/include/c++/4.8.2/bits/unique_ptr.h:65:22:错误:“sizeof”对不完整类型“B”的无效应用
据我了解,编译器告诉我这 B是一个不完整的类型(in main.cpp),因此它无法B正确删除。
但是,在我的设计中,我不想main.cpp拥有完整的B.
粗略地说,这是一个粉刺。
有没有好的解决方法?
这里有一些类似的问题,但没有一个提出干净的解决方法。
.cpp( #1)std::unique_ptr<B>通过使用非模板类 封装显示出丑陋的解决方法。然后,手动将封装器的析构函数移动到.cpp。( #2)#3)是std::unique_ptr不是一个合适的工具?
我应该创建自己的吗unique_ptr?我可以用作#3指南。
Kerrek SB实际上已经解决了代码中的所有问题。非常感谢!
剩下的问题是:-
我应该创建自己的unique_ptr来删除此限制吗?
是否可以?- 我想这是可能的,我现在正在尝试编码。
| 归档时间: |
|
| 查看次数: |
184 次 |
| 最近记录: |