C ++:Vector3类型“wall”? [英] C++: Vector3 type "wall"?

查看:150
本文介绍了C ++:Vector3类型“wall”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有:

class Vector3 {
  float x, y, z;
  ... bunch of cuntions ..
  static operator+(const Vector3&, const Vector3);
};

现在,假设我想要类:

Position, Velocity, 

(基本上,我想要

typedef Vector3 Position;
typedef Vector3 Velocity;

除非:

Position position;
Vector3 vector3;
Velocity velocity;

以下不能发生:

position + vector3;
vector3 + velocity;
velocity + position;

最好的方法是什么?

推荐答案

我会推荐这样的:

template<typename tag>
class Vector3 {
  float x, y, z;
  ... bunch of functions ..
  static operator+(const Vector3&, const Vector3);
};

struct position_tag {};
struct velocity_tag {};
typedef Vector3<position_tag> Position;
typedef Vector3<velocity_tag> Velocity;

这里有一个更复杂的例子: Boost MPL

See here for a more elaborate example: Boost MPL

这篇关于C ++:Vector3类型“wall”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆