我正在编写一个简单的库来处理跨不同平台的窗口创建。为了抽象出特定于平台的代码,我想使用带有std :: unique_ptr的PImpl惯用语,并提供具有工厂功能的特定于平台的私有实现。这是我目前拥有的:
Window.h:
#pragma once
#include <memory>
class Window
{
public:
Window();
~Window();
private:
class WindowImpl;
std::unique_ptr<WindowImpl> impl;
};
Window.cpp:
#include "Window.h"
#include "WindowImpl.h"
Window::Window() : impl(WindowImpl::create()) {}
Window::~Window() = default;
WindowImpl.h:
#include "Window.h"
#include <memory>
class Window::WindowImpl
{
public:
static std::unique_ptr<WindowImpl> create();
};
WindowImpl.cpp:
#include "WindowImpl.h"
std::unique_ptr<Window::WindowImpl> Window::WindowImpl::create()
{
// the plan is to return the platform specific WindowImpl implementation here
return std::make_unique<Window::WindowImpl>();
}
这似乎可以满足我的要求。我的问题学家认为,我目前基本上必须在WindowImpl.cpp中的所有内容前面都指定“ Window ::”。由于Window是类而不是名称空间,因此“使用Window”不起作用。我发现的有关该主题的示例在其类的源文件中都有完整的Impl类定义,因此不存在此问题。但是,如果我想从中获取平台特定的实现,则需要在单独的标头中包含WindowImpl。
有没有一种方法可以将WindowImpl保留在其自己的头文件/源文件中并摆脱这种冗长的词?]
我正在编写一个简单的库来处理跨不同平台的窗口创建。为了抽象出特定于平台的代码,我想使用带有std :: unique_ptr的PImpl习惯用法,并提供一个...
您可以使用类型别名: