parent
dc13493a45
commit
13208c32eb
@ -0,0 +1,16 @@
|
||||
#include "car.h"
|
||||
|
||||
int main()
|
||||
{
|
||||
Driver driver{"老王"};
|
||||
Tyre tyre{"倍耐力"};
|
||||
Car car1{tyre, driver};
|
||||
car1.print(); // 正常
|
||||
|
||||
// 演示聚合可能的问题
|
||||
Driver *temp = new Driver{"临时"}; // 动态申请的司机对象
|
||||
Car car2{tyre, *temp};
|
||||
car2.print(); // 正常
|
||||
delete temp; // 释放司机对象
|
||||
car2.print(); // 触发未定义行为
|
||||
}
|
Loading…
Reference in new issue