C++基礎-多線程擴展繼承(public:thread)


對thread進行繼承,這里overite thread的構造方法 

 Huathread():thread(){ //子類調用父類的方法

    };
    template<typename T, typename...Args> //子類調用父類的構造函數, 可變參數的構造
    Huathread(T && func, Args &&...args):thread(std::forward<T>(func),forward<Args>(args)...)
    {

    }

定義我們自己的方法

  void run(const char* cmd) //新增功能
    {
        system(cmd);
    }

完整代碼

//
// Created by Administrator on 2021/6/27.
//
#include<thread>
#include<iostream>

using namespace std;

class Huathread : public thread{
public:
    Huathread():thread(){ //子類調用父類的方法

    };
    template<typename T, typename...Args> //子類調用父類的構造函數, 可變參數的構造
    Huathread(T && func, Args &&...args):thread(std::forward<T>(func),forward<Args>(args)...)
    {

    }

    void run(const char* cmd) //新增功能
    {
        system(cmd);
    }

};
int main()
{
    Huathread t1([](){
        cout << "hello this is huahua" << endl;
    });
    t1.run("calc");

    Huathread t2([](int num){
        cout << "hello this is huahua" << num << endl;
    }, 100);
    t1.run("notepad");
    cin.get();
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM