Click here to Skip to main content
11,581,982 members (79,435 online)
Click here to Skip to main content

Static Member of Derived Class

, 6 Apr 2013 CPOL 5.5K 3
Rate this:
Please Sign up or sign in to vote.
How to have a single declaration of a static member for derived classes, using a simple template

Introduction

Recently I had a situation where I had a large number of classes all derived from a single base class and wanted each derived class to have a static member of the same type, but with different values. I could obviously not put the static member in the base class otherwise it would be static to all derived classes, not each derived class. Also, the thought of copying and pasting the same static member declaration into 50 classes felt completely wrong. A neat solution is to use a template class to define the derived class.

Using the code 

Here is a quick example of the pattern, which probably has some name if someone feels like enlightening me Smile | :)

Firstly, a simple base class:

class Base
{
public:
    virtual void doSomething() = 0;
};

And then we can define our template class, that is going to hold a static list of something that needs to be the same for each derived class, not all derived classes:

template<typename T>
class DerivedTemplate : public Base
{
protected:
    static std::vector<int> listOfSomething;
};

template <typename _T>
std::vector<int> DerivedTemplate<_T>::listOfSomething;

Now we can write our derived classes, using this template as a base class, rather than the actual Base class: 

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;
    };
};

Nothing particularly clever, but quite useful in my situation, where I have a load of classes for processing XML elements and want a means of managing supported attributes on those elements.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

jsolutions_uk
Software Developer jSolutions Ltd
United Kingdom United Kingdom
John Cumming has been working in software engineering for over 15 years. With qualifications in a mechanical engineering discipline and initial employment in chemical engineering research, he has engineering experience and knowledge to bring to a variety of software projects.

John has a wide range of experience in a variety of technologies, including web applications, distributed architectures and desktop applications and has applied his knowledge and experience in a variety of roles in R&D projects, Integration Consultancy and Enterprise Architecture.

John is experienced in Agile development processes such as XP and Scrum and is a Certified Scrum Professional.

* C++ - UNIX, Windows, COM, MFC, ATL, CLI, CORBA, Qt, boost
* C# - .NET 4.5, WPF, Prism, XAML
* XML - XML, XSLT, XSD, SOAP, XSL-FO
* Security – Cryptography, Key Management, Smart Cards
* Agile and Scrum

LinkedIn
Facebook
jSolutions
Group type: Organisation (No members)



You may also be interested in...

Comments and Discussions

 
QuestionDoes DerivedTemplate need to derive from Base? Pin
Yiannis Spyridakis9-Apr-13 2:09
memberYiannis Spyridakis9-Apr-13 2:09 
AnswerRe: Does DerivedTemplate need to derive from Base? Pin
jsolutions_uk9-Apr-13 3:16
groupjsolutions_uk9-Apr-13 3:16 
GeneralRe: Does DerivedTemplate need to derive from Base? Pin
Yiannis Spyridakis9-Apr-13 3:30
memberYiannis Spyridakis9-Apr-13 3:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150603.1 | Last Updated 6 Apr 2013
Article Copyright 2013 by jsolutions_uk
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid