问题:类B公有继承类A,类A有虚函数fun,类B覆盖了虚函数fun,有一个std::vector<A>,添加A的对象a,和B的对象b,到这个容器里,然后从vector里取出来,使用对象a.fun(),和对象b.fun(),会发生什么?

发现对象b.fun()的调用,实际调用的是父类A的fun()方法。也就是子类部分被切掉了。

如何解决呢,用std::vector<A*>或者std::vector<std::shared_ptr<A>> ,但推荐用智能指针。

Quote3.h

#ifndef __QUOTE3_H__
#define __QUOTE3_H__

#include <iostream>
#include <memory>

class Quote{
 public:
  Quote() = default;
  Quote(const std::string& book, double pri)
    :bookNo(book), price(pri){}
  std::string isbn() const{return bookNo;}
  virtual double net_price(std::size_t n)const{
    return n * price;
  }
  virtual void debug()const{
    std::cout << bookNo << " " << price << std::endl;
  }
  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 qyn, double disc):Quote(book, price),
    quantity(qyn), discount(disc){}
  
  double net_price(std::size_t) const override = 0;
 protected:
  std::size_t quantity = 0;//折扣适用的数量
  double discount = 0.0;   //折扣率
};

class Bulk_quote : public Disc_quote{
 public:
  
  Bulk_quote() = default;
  
  Bulk_quote(const std::string& book, double price,
  std::size_t qyn, double disc)
  :Disc_quote(book, price, qyn, disc){}
  
  double net_price(std::size_t) const override;
};

class Min_quote : public Disc_quote{
 public:

  Min_quote() = default;
  Min_quote(const std::string& book, double price,
       std::size_t qyn, double disc)
   :Disc_quote(book, price, qyn, disc){}
  
  double net_price(std::size_t) const override;
};

#endif

Quote3.cpp

#include "Quote3.h"

double Bulk_quote::net_price(std::size_t cnt) const{
  if(cnt >= quantity){
    return cnt * (1 - discount) * price;
  }
  else{
    return cnt * price;
  }
}


double Min_quote::net_price(std::size_t cnt) const{
  if(cnt < quantity){
    return cnt * (1 - discount) * price;
  }
  else{
    return cnt * price;
  }
}

main.cpp

#include "Quote3.h"
#include <vector>

double print_total(std::ostream& os,
           const Quote& item, size_t n){
  double ret = item.net_price(n);
  os << "ISBN: " << item.isbn()
     << " # sold: " << n << " total due: " << ret << std::endl;
  return ret;

}

int main(){

  std::vector<Quote> qv;
  for(unsigned i = 0; i != 10; ++i){
    qv.push_back(Bulk_quote("01", 100, 10, 0.1));
  }
  double total = 0;
  for(const auto s : qv){
    total += s.net_price(20);
  }
  std::cout << total << std::endl;

  std::cout << "-------------------------" << std::endl;

  std::vector<std::shared_ptr<Quote>> sv;
  for(unsigned i = 0; i != 10; ++i){
    sv.push_back(std::make_shared<Bulk_quote>("01", 100, 10, 0.1));
  }
  double total1 = 0;
  for(const auto s : sv){
    total1 += s->net_price(20);
  }
  std::cout << total1 << std::endl;
  
}

执行结果:

20000
-------------------------
18000

std::vecto<Quote>的执行结果为:20000;std::vector<std::shared_ptr<Quote>>部分的执行结果:18000。所以在容器里放智能指针,可以解决上面的问题。

c/c++ 学习互助QQ群:877684253

本人微信:xiaoshitou5854

内容来源于网络如有侵权请私信删除
你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!

相关课程