【发布时间】:2016-02-07 13:47:49
【问题描述】:
我有一个类,它广泛使用特定命名空间的成员,如下所示:
class Entity {
using namespace glm;
public:
Entity(vec3 position, vec3 direction, vec3 upVector, vec3 velocity, float speed = 0.0f);
Entity(vec3 position, vec3 direction, vec3 upVector);
Entity(vec3 position, vec3 direction);
virtual ~Entity() {}
vec3 position() { return this->pos; }
vec3 direction() { return this->dir; }
vec3 upVector() { return this->upVec; }
vec3 velocity() { return this->vel; }
float speed() { return this->spd; }
// lots of other methods
protected:
vec3 pos;
vec3 dir;
vec3 upVec;
vec3 vel;
float spd;
// lots of other members
};
我刚刚发现using namespace 不允许在课程中使用,所以我不能这样做。
我只能看到 2 个选项如何摆脱这种情况,这两个选项都很愚蠢:
- 在每次使用成员(vec3、vec4、mat3、...)之前重复
namespace_name::(glm::) - 在类外声明
using namespace并将这个命名空间强制给每个人,包括我的标题
有没有更好/更干净的方法,如何解决这个问题?
【问题讨论】:
-
使用 typedef?类似于 typedef glm::X glmX ?
-
在类中使用 glm::vec3 会起作用
-
为什么 1 有问题?我会说这是最佳实践,也是最易读的。
-
我不介意写命名空间,如果只有少数使用它的成员的情况,但是当它有很多时 1. 一段时间后它开始写它真的很烦人到处。 2. 看起来很丑,代码向右展开到屏幕外。 3. glm 库旨在提供与 OpenGL 着色语言相同的工具,它使用这些东西作为 vec3,mat3,... 没有任何前缀。
标签: c++ class namespaces header-files