You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

16 lines
391 B

#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(); // 触发未定义行为
}