我有一个依赖于"Blibrary"的图书馆"myLibrary".我希望"myLibrary"的用户不知道这种依赖.我试图隐瞒它没有运气,这是我现在所拥有的一个例子.
#include <game/Object.h>
#include <Blibrary/Component.hpp> // How can I remove this library header?
// forward declaring it? it's a template..
namespace myLibrary {
template<typename T>
struct Component: public Object, public Blibrary::Component<T>
{
};
//template<typename T>
//class Blibrary::Component<T>; //I Tried something like that..
//template<typename T>
//struct Component: public Object
//{
// Blibrary::Component<T> * m_impl;
//};
}
//I want the user do this when declaring a usermade component:
#include <game/Component.h> //<-- but without the Blibrary include dependency
class Position: public myLibrary::Component<Position>
{
float x, y, z;
};
Run Code Online (Sandbox Code Playgroud)
R S*_*ahu 10
是否可以隐藏模板类的实现?
不它不是.必须在头文件中完全定义类模板.您只能通过使用多层头文件并使用辅助类名和辅助函数名来混淆实现,辅助函数名是最高级别用户可见类的混淆.
但是,正如@vsoftco在评论中指出的那样,如果仅将其用于某些特定类型,则可以隐藏它,在这种情况下,您可以执行显式实例化,导出模板并在.cpp中实现它.