Dependent Managent(环形依赖)

软件开发有两种依赖方式:

一种是编译时依赖,就是在b.h文件中添加#include“a.h”

第二种是链接时依赖,是指生成的目标文件a.o在查找可用符号时要去别的目标文件中去查找。例如,在a.h中存在extern int n;那么在生成的a.o文件里就需要去别的目标文件中查找n的定义,这就构成了链接时依赖。

假定存在两个类CLASS A  CLASS B.(在UML中,依赖关系用--------->表示)

如果在这两个类之间发生依赖关系的话,存在以下几种方式:

1)class A中含有B类性或者B指针类型的变量。

2)classA派生自classB

3)classA中某个函数需要classB类型参数。

4)classA中某个函数需要classB类型的static 参数。

5)classA需要给classB传递消息。

在以上几种情况,都需要在classA的cpp文件中,添加#include<classB.h>.

代码重用,作为重要的设计目标,必然产生依赖。在你添加#include<classB.h>的同时,也将classB.h中的头文件也相应的添加了进来,这样大大增加了compile time,而且最大的问题是,容易发生circular dependency,导致编译不通过,报错:“找不到基类”。

PS:环形依赖是请参考http://en.wikipedia.org/wiki/Circular_dependency

解决方法:在某一个环节中, 采用forward declaration(前向声明)的方式,来打断由于#include<>所带来的环。

原文:

A forward declaration of a class declares its name as a valid class name but leaves out its definition. This permits that name to be used as a type for pointers and references that are not dereferenced before the definition is encountered. Forward declarations make it possible for classes to have circular relationships without having circular dependencies between header files (which the compiler will not permit).


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值