我已经使用了前向声明;他们帮助避免了许多问题,提高了编译时间,而不是什么。但是如果我想要在标准库中声明一个类呢?涉及std :: vector的前向声明
// Prototype of my function - i don't want to include <vector> to declare it!
int DoStuff(const std::vector<int>& thingies);
我听说它是被禁止/不可能前瞻性声明std::vector
。现在this answer to an unrelated question建议以这种方式重写我的代码:
stuff.h
class VectorOfNumbers; // this class acts like std::vector<int>
int DoStuff(const VectorOfNumbers& thingies);
stuff.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
的收益是否超过它们?
不能VectorOfNumbers只是提供一个转换ctor从它的底层std :: vector类型? –
@Martin:它会复制整个事情。你不想要这个,是吗? – ybungalobill