我经常使用前向声明;它们有助于避免许多#include
s,缩短编译时间等等。但是如果我想在标准库中前向声明一个类怎么办?
// Prototype of my function - i don't want to include <vector> to declare it!
int DoStuff(const std::vector<int>& thingies);
我听说 forward-declare 是禁止/不可能的std::vector
。现在这个对一个不相关问题的回答建议用这种方式重写我的代码:
东西.h
class VectorOfNumbers; // this class acts like std::vector<int>
int DoStuff(const VectorOfNumbers& thingies);
东西.cpp
// Implementation, in some other file
#include <vector>
class VectorOfNumbers: public std::vector<int>
{
// Define the constructors - annoying in C++03, easy in C++11
};
int DoStuff(const VectorOfNumbers& thingies)
{
...
}
现在,如果我在整个项目中使用VectorOfNumbers
而不是std::vector<int>
在所有上下文中使用,一切都会好起来的,我不再需要#include <vector>
在我的头文件中!
这种技术有很大的缺点吗?能够提前申报的收益能否vector
超过它们?