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 }
  • 相关阅读:
    算法笔记4
    算法笔记3
    SQLServer 两种分页方式
    Git 使用笔记
    Docker MongoDB 笔记
    Abp vNext 修改Identity模块用户管理界面的坑
    CentOS安装MariaDB
    Visio修改页边距
    在 PowerDesigner 导入Excel中的表结构
    Uploadify上传失败,提示 2156 SecurityError Error #2156 null的解决办法
  • 原文地址:https://www.cnblogs.com/xiaochi/p/8593642.html
Copyright © 2011-2022 走看看