解决明显的循环依赖性:A类具有接收B类的方法,而B类具有A类A类
我有两个我想定义的类,position
和tangentVector
,部分给出如下:
class Position
{
public:
Position(double x, double y, double z);
// getters
double x(){ return m_x };
double y(){ return m_x };
double z(){ return m_x };
void translate(const TangentVector& tangent_vector);
private:
double m_x;
double m_y;
double m_z;
}
class TangentVector
{
public:
Tangent(double x, double y, double z, Position position);
// getters
double x(){ return m_x };
double y(){ return m_x };
double z(){ return m_x };
private:
double m_x;
double m_y;
double m_z;
Position m_position;
}
使用类别的关键内容是TangentVector
>具有类型位置
的成员类型的参数const tangentVector&
(位置
取决于tangentVector
?)。
出于上下文的考虑,位置
旨在表示单位球体上的点,contentVector
描述了与球体切线的向量,并用a <指定的向量的起源代码>位置。由于veTortAngent
的定义需要指定位置
,因此可以说vectortangent
取决于位置
>。但是,现在我想定义一个在球体上采用位置
的函数,并通过contentVector
给出的方向和距离沿球体“翻译”它。我真的很喜欢这个翻译
方法以位置
类生活,因为它是修改position
的状态的函数。这将适合以下用法,我认为这很自然:
Position position{ 1.0, 0.0, 0.0 };
TangentVector tangent_vector{ 0.0, PI/2, 0.0, position };
position.translate(tangent_vector); // Now { 0.0, 1.0, 0.0 };
但是,我担心这会导致某些循环依赖。那么...
- 这种情况是循环依赖性的例子吗?这种情况是不好的习俗吗?
- 如果是这样,如何避免这种循环依赖性?如何修改此代码以使其与良好的OOP实践融为一体?
(我考虑过将位置m_position
成员改为原始指针。但是,在这种情况下,我打算m_position
将由tangentVector
,我不允许在示例使用代码中更改其外部的可能性,而是希望翻译
方法修改tangent_vector
代码> tangent_vector 的构造函数将位置
作为指针,并将其存储为会员。)
I have two classes that I want to define, Position
and TangentVector
, partially given as follows:
class Position
{
public:
Position(double x, double y, double z);
// getters
double x(){ return m_x };
double y(){ return m_x };
double z(){ return m_x };
void translate(const TangentVector& tangent_vector);
private:
double m_x;
double m_y;
double m_z;
}
class TangentVector
{
public:
Tangent(double x, double y, double z, Position position);
// getters
double x(){ return m_x };
double y(){ return m_x };
double z(){ return m_x };
private:
double m_x;
double m_y;
double m_z;
Position m_position;
}
The key thing to note with the classes is that TangentVector
has a member of type Position
(TangentVector
depends on Position
) while Position
has a method that takes in an argument of type const TangentVector&
(Position
depends on TangentVector
?).
For context's sake, Position
is intended to represent a point on the unit sphere, and TangentVector
describes a vector tangent to the sphere, with the origin of the vector specified by a Position
. Since the definition of a VectorTangent
requires a Position
to be specified, it seems reasonable to say that VectorTangent
depends on Position
. However, now I want to define a function that takes a Position
on the sphere, and "translates" it along the sphere by a direction and distance given by TangentVector
. I would really like this translate
method to live in the Position
class, since it a function that modifies the state of Position
. This would lend itself to the following usage, which I feel is fairly natural:
Position position{ 1.0, 0.0, 0.0 };
TangentVector tangent_vector{ 0.0, PI/2, 0.0, position };
position.translate(tangent_vector); // Now { 0.0, 1.0, 0.0 };
However, I fear that this results in some circular dependency. So...
- Is this case an example of circular dependency? Is this case bad practice?
- If so, how can this circular dependency be avoided? How can this code be modified such that it is in-line with good OOP practices?
(I considered making the Position m_position
member a raw pointer instead. In this case, however, I intend m_position
to be fully owned by TangentVector
, rather than allow the possibility of it being altered external to the class. In the example usage code, I do not want the translate
method to modify tangent_vector
, which would happen if tangent_vector
's constructor took in position
as a pointer and stored it as a member.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
类位置
仅参考类TangentVector
。因此,在声明类位置
:class Position
takes only a reference toclass TangentVector
. Therefore you might pre-declareTangentVector
asclass TangentVector;
before the declaration ofclass Position
: