解决明显的循环依赖性:A类具有接收B类的方法,而B类具有A类A类

发布于 2025-02-11 04:44:12 字数 1861 浏览 1 评论 0原文

我有两个我想定义的类,positiontangentVector,部分给出如下:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遥远的她 2025-02-18 04:44:12

类位置仅参考类TangentVector。因此,在声明类位置

class 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 Position takes only a reference to class TangentVector. Therefore you might pre-declare TangentVector as class TangentVector; before the declaration of class Position:

class 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;
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文