假设 Acme 公司发布了一个有用的库,其中包含一个极其丑陋的 C API。我想将结构和相关函数包装在 C++ 类中。似乎我不能为包装类使用相同的名称,因为原始库不在命名空间内。
这样的事情是不可能的,对吧?
namespace AcmesUglyStuff {
#include <acme_stuff.h> // declares a struct Thing
}
class Thing {
public:
...
private:
AcmesUglyStuff::Thing thing;
};
链接将是一个问题。
我能想到的包装库的唯一方法,而不是用 C 库名称污染我的命名空间,是这样的 hack,在类中保留空间:
// In mything.h
namespace wrapper {
class Thing {
public:
...
private:
char impl[SIZE_OF_THING_IN_C_LIB];
};
}
// In thing.cc
#include <acme_stuff.h>
wrapper::Thing::Thing() {
c_lib_function((::Thing*)impl); // Thing here referring to the one in the C lib
}
这是唯一的方法吗?我想避免在我的所有类名上加上前缀,比如XYThing
等。