c++(1) 類模板/建構函式/捕獲異常 執行個體__函數

來源:互聯網
上載者:User

title: c++(1) 類模板/建構函式/捕獲異常 執行個體
tag: cpp

在一個標頭檔裡,一般上面寫模板類的聲明,下面寫模板類的定義
一、類模板 1.模板類的聲明

template<typename TDatums,           typename TWorker = std::shared_ptr<Worker<std::shared_ptr<TDatums>>>,        typename TQueue = Queue<std::shared_ptr<TDatums>>>  class Wrapper
2.模板類的使用

這裡有三種形式參數,調用的時候傳入任何一種參數即可。建立了一個類的對象,opWapper。

 op::Wrapper<std::vector<op::Datum>> opWrapper;

std::vector<op::Datum>應該屬於第一種形參 3.模板函數的定義

代碼中有一行調用了configue這個函數。
configure為名定義的同名函數就有四個,這四個所傳入的參數個數,類型不同,因此可以重名。
opWrapper.configure(wrapperStructPose, wrapperStructFace, wrapperStructHand, wrapperStructInput, wrapperStructOutput); 二、建構函式 1.聲明

explicit Wrapper(const ThreadManagerMode threadManagerMode = ThreadManagerMode::Synchronous); 2.定義

建構函式用冒號初始化一些變數

    template<typename TDatums, typename TWorker, typename TQueue>    Wrapper<TDatums, TWorker, TQueue>::Wrapper(const ThreadManagerMode threadManagerMode) :        mThreadManagerMode{threadManagerMode},        spVideoSeek{std::make_shared<std::pair<std::atomic<bool>, std::atomic<int>>>()},        mThreadManager{threadManagerMode},        mMultiThreadEnabled{true}    {        try        {            // It cannot be directly included in the constructor, otherwise compiler error for copying std::atomic            spVideoSeek->first = false;            spVideoSeek->second = 0;        }        catch (const std::exception& e)        {            error(e.what(), __LINE__, __FUNCTION__, __FILE__);        }    }
3.解構函式 定義
 template<typename TDatums, typename TWorker, typename TQueue> Wrapper<TDatums, TWorker, TQueue>::~Wrapper() {函數體}
一般的函數定義
template<typename TDatums, typename TWorker, typename TQueue>void Wrapper<TDatums, TWorker, TQueue>::disableMultiThreading(){函數體}

在類模板中,所以函數定義的時候都要加上
template<typename TDatums, typename TWorker, typename TQueue> 模板關鍵字
和Wrapper<TDatums, TWorker, TQueue>:: 類名 三、捕獲異常

try…catch 語句將能引發錯誤的代碼放在try塊中,並且對應一個響應,然後有異常被拋出。

   template<typename TDatums, typename TWorker, typename TQueue>    void Wrapper<TDatums, TWorker, TQueue>::configure(const WrapperStructPose& wrapperStructPose,  const WrapperStructFace& wrapperStructFace, const WrapperStructInput& wrapperStructInput,const WrapperStructOutput&wrapperStructOutput)    {        try        {            configure(wrapperStructPose, wrapperStructFace, WrapperStructHand{},                      wrapperStructInput, wrapperStructOutput);        }        catch (const std::exception& e)        {            error(e.what(), __LINE__, __FUNCTION__, __FILE__);        }    }

這個configure函數只有四個輸入,但是我們看它函數體內部,其實還是指向有五個輸入的那個函數,並寫了一個捕獲異常

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.