c – 如何摆脱在类头中重复编写namespace_name ::
作者:互联网
我有一个类,它广泛使用特定命名空间的成员,如下所示:
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
};
我刚刚发现在类中不允许使用命名空间,所以我不能这样做.
我只能看到2个选项如何摆脱这个,这两个都是愚蠢的:
>在每次使用成员(vec3,vec4,mat3,…)之前重复namespace_name ::(glm::)
>在类之外声明使用命名空间,并将此命名空间强制给包含我的标题的所有人
是否有更好/更清洁的方式,如何解决这个问题?
解决方法:
作为一个良好的实践,您自己的类也应该在命名空间中.你可以把using语句放在那里.
namespace MyProject
{
using namespace glm;
class Entity
{
...
};
}
标签:c,class,header-files,namespaces 来源: https://codeday.me/bug/20190824/1707512.html