zoukankan      html  css  js  c++  java
  • 88.类的静态成员,以及继承多线程类的实现

     1 #include <iostream>
     2 #include <thread>
     3 using namespace std;
     4 
     5 class myclass
     6 {
     7 public:
     8     //static只会初始化一次
     9     static int num;
    10     myclass()
    11     {
    12         num += 1;
    13     }
    14     ~myclass()
    15     {
    16         num -= 1;
    17     }
    18 };
    19 21 //静态成员不属于任何一个对象,任何对象都可以访问
    22 //用于同类对象通信
    23 int myclass::num = 3;
    24 
    25 class mythread : public thread
    26 {
    27 public:
    28     static int count;
    29 
    30 public:
    31     //重载两个构造函数
    32     mythread() : thread()//子类调用父类构造函数
    33     {
    34 
    35     }
    36     template<typename T,typename...Args>
    37     mythread(T &&func, Args &&...args) : thread(forward<T>(func), forward<Args>(args)...)
    38     {
    39 
    40     }
    41 };
    42 
    43 int mythread::count = 0;
    44 
    45 void go()
    46 {
    47     while(1)
    48     {
    49         mythread::count += 1;
    50         cout << this_thread::get_id() << " " << mythread::count << endl;
    51         this_thread::sleep_for(chrono::seconds(3));
    52     }
    53     
    54 }
    55 
    56 void main()
    57 {
    58     //myclass my1, my2, my3;
    59     //cout << my1.num << endl;
    60     //cout << my2.num << endl;
    61     //cout << my3.num << endl;
    62 
    63     mythread my1(go);
    64     mythread my2(go);
    65     mythread my3(go);
    66     cin.get();
    67 }
  • 相关阅读:
    Tree Recovery解题报告
    bjtuOJ1019 Robot
    bjtuOJ1137 蚂蚁爬杆
    栈的使用,rails
    重做catch the cow
    C#3.0新特性之匿名类型
    C#Lambda表达式的用法
    C#进程的使用方法详解
    C#进程管理启动和停止
    C#LINQ查询表达式用法
  • 原文地址:https://www.cnblogs.com/xiaochi/p/8593642.html
Copyright © 2011-2022 走看看