C++ 多态性的深入解析与应用
1. 多态性基础回顾
在调用 volume() 函数时,除了一种情况外,默认参数值输出通常是基类函数所指定的。唯一的例外是使用 hardcase 对象调用 volume() 时,它会静态解析为 ToughPack 类中的 volume() 函数,因此会使用 ToughPack 类中指定的默认参数值。而其他调用则是动态解析的,即使执行的函数位于派生类中,也会应用基类中指定的默认参数值。
2. 使用智能指针调用虚函数
多态性在使用智能指针时同样有效。以下是一个示例代码:
// Ex14_05.cpp
// Virtual functions using smart pointers
#include <iostream>
#include <memory> // For smart pointers
#include <vector> // For vector
#include "Box.h" // For the Box class
#include "ToughPack.h" // For the ToughPack c
超级会员免费看
订阅专栏 解锁全文
302

被折叠的 条评论
为什么被折叠?



