Let’s say I have an std::vector of std::strings.
// Foo.h
class Foo {
std::vector< std::string > mVectorOfFiles;
}
Then I used typedef to make it a StringVector type.
// Foo.h
typedef std::vector< std::string > StringVector;
class Foo {
StringVector mVectorOfFiles;
}
If I have another class which takes a StringVector object…
// Bar.h
class Bar {
Bar( const StringVector & pVectorOfFiles ); // I assume this produces a compile error (?) since Bar has no idea what a StringVector is
}
… do I have to use typedef again in the header file for Bar?
// Bar.h
typedef std::string< std::vector > StringVector;
class Bar {
Bar( StringVector pListOfFiles );
}
Is it possible to place the typedef std::vector< std::string > StringVector in a single file and have every other class know of the type StringVector?
All files that
#include "Foo.h"you get thetypedef. So no, you don’t have to replicate it in every file (as long as it includesFoo.h. You can place thetypedefin a dedicated file if that suits your needs. In your case, this would make sense and would be an improvement, becauseBar.hshould not rely on the fact thatFoo.hhas thetypedefand the necessary includes.I would keep it simple and limit it to one type though, to be included in all files that use the type: