在多個命名空間中使用相同的類名會使我陷入麻煩嗎?我也試着去除依賴到數學庫。你怎麼看待下面的設計。類命名和命名空間
第一個文件
#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