私は前方宣言を多く使用しました。彼らは多くの#include
を回避し、コンパイル時間を改善し、何を改善するのに役立ちます。しかし、もし私が標準ライブラリのクラスを前方宣言したいのであれば?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
に/不可能禁じられています聞いたことがあります。私はすべてのコンテキストでVectorOfNumbers
代わりのstd::vector<int>
を使用する場合、今
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)
{
...
}
:今this answer to an unrelated questionは私のコードをこのように書き換えることを提案します私のプロジェクトを通して、すべてがうまくいくようになりました。私のヘッダファイルには#include <vector>
はもう必要ありません。
この手法には大きな欠点がありますか? vector
が前方に宣言できるという利得はそれを上回ることはありますか?
VectorOfNumbersは基本的なstd :: vector型の変換タグを自明に提供できませんでしたか? –
@マーティン:それはすべてをコピーします。あなたはこれを望んでいないのですか? – ybungalobill