在多个命名空间中使用相同的类名会使我陷入麻烦吗?我也试着去除依赖到数学库。你怎么看待下面的设计。类命名和命名空间
第一个文件
#define MATH_RECTANGLE_EXISTS
namespace math {
class Rectangle : Object2D {
public:
float perimeter();
float area();
float x,y,w,h;
};
}
其他文件
#define GRAPHIC_RECTANGLE_EXISTS
#ifndef MATH_RECTANGLE_EXISTS
//is this a good idea to remove dependency?
namespace math {
class Rectangle {
public:
float x,y,w,h;
}
}
#endif
namespace graphics {
class Rectangle : math::Rectangle {
public:
void Draw(Canvas &canvas);
void Translate(float x, float y);
};
}
编辑
怎么样这种方法来删除依赖?
** 1 **文件
namespace common {
class Rectangle {
float x,y,w,h;
};
}
数学库文件
#define MATH_RECTANGLE_EXISTS
namespace math {
class Rectangle : public common::Rectangle, public Object2D {
public:
float perimeter();
float area();
};
}
图形文件
#define GRAPHIC_RECTANGLE_EXISTS
namespace graphics {
#ifndef MATH_RECTANGLE_EXISTS
class Rectangle : public math::Rectangle {
#else
class Rectangle : public common::Rectangle {
#endif
public:
void Draw(Canvas &canvas);
void Translate(float x, float y);
};
}
在此先感谢。
是的,我意识到这一点,但我不想要两个库之间的依赖关系,而我想在它存在时使用它的功能。 – 2009-10-04 16:32:44
我已经更新了这个问题,你能否再次检查一下,thx。 – 2009-10-04 16:37:45
然后你应该使用某种接口/工厂。如果数学库可用,则可以从中取出实现,否则将使用替换。 – sdg 2009-10-04 16:38:34