對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(); }
