zoukankan      html  css  js  c++  java
  • Static Member of Derived Class

    Static Member of Derived Class 原文 From http://www.codeproject.com/ by jsolutions_uk

    View Code
    #include <vector>
    #include <iostream>
    
    using namespace std;
    
    class Base
    {
    public:
        virtual void doSomething() = 0;
    };
    
    template<typename T>
    class DerivedTemplate : public Base
    {
    protected:
        static std::vector<int> listOfSomething;
    };
    
    template <typename _T>
    std::vector<int> DerivedTemplate<_T>::listOfSomething;
    
    class DerivedOne: public DerivedTemplate<DerivedOne>
    {
    public:
        DerivedOne()
        {
            if(listOfSomething.empty())
            {
                listOfSomething.push_back(1);
                listOfSomething.push_back(2);
            }
        };
        
        virtual void doSomething()
        {
            std::cout << "DerivedOne contains " << listOfSomething.size() << " items." << std::endl;
        };
    };
    
    class DerivedTwo: public DerivedTemplate<DerivedTwo>
    {
    public:
        DerivedTwo()
        {
            if(listOfSomething.empty())
            {
                listOfSomething.push_back(3);
                listOfSomething.push_back(4);
                listOfSomething.push_back(5);
            }
        };
        
        virtual void doSomething()
        {
            std::cout << "DerivedTwo contains " << listOfSomething.size() << " items." << std::endl;
        };
    };
    
    int main(int argc, char* argv[])
    {
        DerivedOne oDerivedOne;
        DerivedTwo oDerivedTwo;
        
        oDerivedOne.doSomething();
        oDerivedTwo.doSomething();
        
        return 0;
    }
  • 相关阅读:
    CF1324F Maximum White Subtree——换根dp
    bzoj3029 守卫者的挑战
    k8s-pod
    k8s 介绍
    docker-dockerfile
    docker学习
    git
    windows 上git安装及gitlab 连接
    gitlab 配置管理
    gitlab安装/配置/维护
  • 原文地址:https://www.cnblogs.com/kongxian/p/3066935.html
Copyright © 2011-2022 走看看