q******8 发帖数: 848 | 1 如何体现composition和aggregation的区别?
比如car有body,engine,tires。但是engine和tries与car应该是aggregation的关系,
那么在类设计的时候是不是应该这样:
class Car{
private:
Body body;
Tire* tries;
Engine engine;
public:
Car(Tire* tries, Engine engine){
body = new Body();
tries = tries;
engine = engine;
}
~Car(){
delete body;
}
}
在销毁car object时候只销毁composition关系的,然后保留aggregation关系的,是这
么写吗? | q******8 发帖数: 848 | | y******n 发帖数: 47 | 3 From wikipedia:
Composition implies the ownership. When the owning object is destroyed, so
are the contained objects. In aggregation, this is not necessarily true. For
example, a university owns various departments (e.g., chemistry), and each
department has a number of professors. If the university closes, the
departments will no longer exist, but the professors in those departments
will continue to exist. Therefore, a University can be seen as a composition
of departments, whereas departments have an aggregation of professors. In
addition, a Professor could work in more than one department, but a
department could not be part of more than one university.
【在 q******8 的大作中提到】 : 求大牛指点。
|
|