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();
}
原文地址:https://www.cnblogs.com/my-love-is-python/p/14940799.html