C++中衍生類別的構造和析構順序詳解

來源:互聯網
上載者:User

衍生類別因為要調用基類, 所以構造和析構都是按照一定順序進行;

構造的順序是: 基(base) -> 派生(derived);即先構造基類, 再構造衍生類別;

因為 基類 是獨立於衍生類別的, 即不會調用衍生類別中的對象, 所以應該先被產生;

如果衍生類別先於基類產生, 則因為無法調用基類資源, 可能產生失敗;

析構的順序是: 派生(derived) -> 基(base); 即先釋放衍生類別, 再釋放基類;

因為 衍生類別 需要先釋放調用的基類資源, 所以應該優先釋放;

如果基類先析構, 則有可能某些資源被衍生類別佔用, 可能導致析構失敗;

衍生類別的構造和析構順序正好相反;

代碼:

/*  * 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() {          std::cout << "this is Quote constructor" << std::endl;      }      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; //動態綁定析構器      virtual ~Quote() {          std::cout << "this is Quote destructor" << std::endl;      }  private:      std::string bookNo;  protected: //受保護類型      double price = 0.0;  };  /*更多精彩內容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/cplus/*/ class Disc_quote : public Quote { //抽象基類  public:      //Disc_quote() = default;      Disc_quote() {          std::cout << "this is Disc_quote constructor" << std::endl;      }      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; //純虛函數      virtual ~Disc_quote() override{          std::cout << "this is Disc_quote destructor" << std::endl;      }  protected:          std::size_t quantity = 0;          double discount = 0.0;  };        class Bulk_quote final : public Disc_quote { //final限定詞, 無法被繼承  public:      //Bulk_quote() = default;      Bulk_quote() {          std::cout << "this is Bulk_quote constructor" << std::endl;      }      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;      virtual ~Bulk_quote() override{          std::cout << "this is Bulk_quote destructor" << std::endl;      }  };        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) {            Bulk_quote* bq = new Bulk_quote;      delete bq;            return 0;        }

輸出:

this is Quote constructor  this is Disc_quote constructor  this is Bulk_quote constructor  this is Bulk_quote destructor  this is Disc_quote destructor  this is Quote destructor

作者:csdn部落格 Spike_King

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.