我正在尝试学习如何使用PIMPL习惯用法,因为它减少了编译依存关系,建议您这样做。所以我有基本上看起来像这样的代码。
oo
class Foo
{
public:
Foo();
private:
class FooImpl;
std::unique_ptr<FooImpl> impl;
}
Foo.cpp
Foo::Foo():
impl(new FooImpl)
{
}
class Foo::FooImpl
{
public:
FooImpl();
}
但是,现在我想像使用一样FooImpl::FooImpl()
在一个单独的.cpp文件中进行定义Foo::Foo()
,但是我将如何去做呢?
编辑:我已经四处移动以获取下面的代码,但是现在初始化impl
给了我一个不完整的类型编译错误。
oo
class Foo
{
public:
Foo();
private:
class FooImpl;
std::unique_ptr<FooImpl> impl;
}
Foo.cpp
#include "Foo.h"
Foo::Foo():
impl(new FooImpl)
{
}
FooImpl.cpp
#include "Foo.h"
class Foo::FooImpl
{
public:
FooImpl();
}
我的解决方案是在Foo类中保留FooImp的定义。
让所有班级成员在那里。然后FooImp.cpp包含Foo.h并实现所有非内联函数。
我是这样工作的:
oo
class Foo
{
public: // ctor & dtor
Foo();
~Foo();
private: // nested class
class FooImp
{
public:
FooImp();
~FooImp();
};
private: // member variable
std::unique_ptr<FooImpl> impl;
};
Foo.cpp
#include "Foo.h"
Foo::Foo()
{
}
Foo::~Foo()
{
}
FooImp.cpp
#include "Foo.h"
Foo::FooImp::FooImp()
{
}
Foo::FooImp::~FooImp()
{
}
它编译良好。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句