容器不支持殽杂范例, 假如直接把派生类工具, 存入基类容器中, 则无法利用派生-基转换(derived-base conversion);
因为转换只能产生在指针和引用 进程中, 不能产生在 工具直接赋值, 假如是直接转换, 则会发生截断(sliced down);
即派生类部门被切除, 只留下基类部门; 所以存入容器中的派生类 输出为基类部门 的虚函数;
假如想在容器中, 举办担任, 则需要利用指针, 包罗智能指针(如:shared_ptr<>), 则会输出派生类的覆写(override)版本的虚函数;
代码:
/* * CppPrimer.cpp * * Created on: 2013.11.12 * Author: Caroline */ /*eclipse cdt*/ #include <iostream> #include <string> #include <vector> #include <memory> #include <cstddef> using namespace std; class Quote { public: Quote() = default; Quote (const std::string& book, double sales_price) : bookNo (book), price (sales_price) {} std::string isbn() const { return bookNo; } virtual double net_price (std::size_t n) const { return n* price; } //虚函数 virtual ~Quote() = default; //动态绑定析构器 private: std::string bookNo; protected: //受掩护范例 double price = 0.0; }; class Disc_quote : public Quote { //抽象基类 public: Disc_quote() = default; Disc_quote (const std::string& book, double price, std::size_t qty, double disc) : Quote(book, price), quantity (qty), discount (disc) {} double net_price (std::size_t) const = 0; //纯虚函数 protected: std::size_t quantity = 0; double discount = 0.0; }; class Bulk_quote final : public Disc_quote { //final限定词, 无法被担任 public: Bulk_quote() = default; Bulk_quote(const std::string& book, double p, std::size_t qty, double disc) : Disc_quote(book, p, qty, disc) {} //利用基类的结构器 double net_price(std::size_t cnt) const override; }; double Bulk_quote::net_price(std::size_t cnt) const { if (cnt >= quantity) return cnt * (1-discount) * price; else return cnt * price; } double print_total(std::ostream &os, const Quote& item, std::size_t n) { double ret = item.net_price(n); os << "ISBN: " << item.isbn() << " # sold: " << n << " total due: " << ret << std::endl; return ret; } int main (void) { //正常存放, 无法挪用派生类的虚函数 std::vector<Quote> basket; basket.push_back(Quote("CppPrimer", 50)); basket.push_back(Bulk_quote("CppPrimer", 50, 10, .25)); std::cout << "Bulk_quote : " << basket.back().net_price(20) << std::endl; //利用指针存放, 可以正确挪用 std::vector<std::shared_ptr<Quote> > sbasket; sbasket.push_back(std::make_shared<Quote>("CppPrimer", 50)); sbasket.push_back(std::make_shared<Bulk_quote>("CppPrimer", 50, 10, .25)); std::cout << "Bulk_quote : " << sbasket.back()->net_price(20) << std::endl; return 0; }
输出:
Bulk_quote : 1000 Bulk_quote : 750
作者:csdn博客 Spike_King