JTo*_*Tom 4 c++ typedef namespaces
我问的是C++项目中广泛使用的最佳实践.我需要在项目中拥有自己的类型.它是几个typedef的集合.
包含头文件包含C++中的类型良好实践,或者使用命名空间更好.如果是这样,为什么?这两种方式的优点和缺点是什么?
现在它看起来像这样:
types.h中:
#ifndef TYPES_H
#define TYPES_H
#include <list>
// forward declaration
class Class;
typedef int TInt;
// ...
typedef std::list<Class> class_list;
#endif
Run Code Online (Sandbox Code Playgroud)
class.h:
#ifndef CLASS_H
#define CLASS_H
#include "types.h"
class Class
{
public:
// ...
TInt getMethod();
private:
// ...
};
Run Code Online (Sandbox Code Playgroud)
如何使用命名空间?
Den*_*ose 10
这两个概念是正交的; 比较它们你提出的方式毫无意义.
除非您只在单个文件中使用这些类型,否则必须将它们放在标题中,以便在需要时轻松引入它们的定义.然后,在该标头中,您可以包含您的命名空间:
#ifndef TYPES_H
#define TYPES_H
#include <list>
namespace MyNamespace {
// forward declaration
class Class;
typedef int TInt;
// ...
typedef std::list<Class> class_list;
}
#endif
Run Code Online (Sandbox Code Playgroud)
然后你可以做,例如,MyNamespace::TInt
而不是int
在你之后#include "Types.h"
归档时间: |
|
查看次数: |
3902 次 |
最近记录: |