没有向前声明所有使用的函数的交叉依赖性? [英] Cross dependencies without forward declaring all used functions?

查看:153
本文介绍了没有向前声明所有使用的函数的交叉依赖性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有A类(在A.h中)取决于(B.h)中的B类,反之亦然。转发声明使用的函数工作,但这意味着我必须更新无处不在,我转发声明这些功能,在未来,ex,如果我删除或更改参数在那些函数,他们必须更新以反映更改。我不觉得这是好的做法。是否有办法解决这个问题?

I have class A (in A.h) which depends on class B in (B.h) and vice versa. Forward declaring the used functions works, but this means I have to update everywhere where I forward declared those functions in the future, ex, if I remove, or change argument in those functions they must all be updated to reflect change. I don't feel this is good practice. Is there a way around this?

谢谢

推荐答案

只需要在声明级别使用指针引用即可,您可以这样做:

If you only need to work with pointers or references to a class at the declaration level, you can do it like this:

class B; // forward class declaration

class A {
    A(B &);
};



Bh



B.h

class A;

class B {
    B(A &);
};



B.cpp



B.cpp

#include "B.h"
#include "A.h" // now we get the full declaration of A

B::B(A &a) {
    a.foo(5);
}

这样的相互依赖性很难处理,但有时是不可避免的。

Mutual dependencies like this are tough to deal with but sometimes unavoidable.

如果 A B / em>,那么您有一个系统设计问题,您需要在继续操作之前解决。

If A and B depend on the implementations of each other, then you've got a system design problem that you need to resolve before proceeding further.

这篇关于没有向前声明所有使用的函数的交叉依赖性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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