比方说,我有一个std::vector的std::string秒.
// Foo.h
class Foo {
std::vector< std::string > mVectorOfFiles;
}
Run Code Online (Sandbox Code Playgroud)
然后我常常typedef把它变成一种StringVector类型.
// Foo.h
typedef std::vector< std::string > StringVector;
class Foo {
StringVector mVectorOfFiles;
}
Run Code Online (Sandbox Code Playgroud)
如果我有另一个拿一个StringVector对象的课......
// Bar.h
class Bar {
Bar( const StringVector & pVectorOfFiles ); // I assume this produces a compile error (?) since Bar has no idea what a StringVector is
}
Run Code Online (Sandbox Code Playgroud)
...我必须typedef在头文件中再次使用Bar吗?
// Bar.h
typedef std::string< std::vector > StringVector;
class Bar {
Bar( StringVector pListOfFiles );
}
Run Code Online (Sandbox Code Playgroud)
是否可以将其typedef std::vector< std::string > StringVector放在一个文件中并让其他所有类都知道该类型StringVector?
jua*_*nza 16
#include "Foo.h"你得到的所有文件typedef.所以,不,你不具备复制它在每一个文件(只要它包括Foo.h,你可以将typedef一个专用的文件是否适合您的需要.在你的情况,这将使感,将是一种进步,因为Bar.h不应该依赖Foo.h具有typedef和必要包括的事实.
我会保持简单,并将其限制为一种类型,但要包含在使用该类型的所有文件中:
// StringVector.h
#ifndef STRINGVECTOR_H_
#define STRINGVECTOR_H_
#include <vector>
#include <string>
typedef std::vector< std::string > StringVector;
#endif
Run Code Online (Sandbox Code Playgroud)