Click here to Skip to main content
12,894,200 members (69,893 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


4 bookmarked
Posted 6 Apr 2013

Static Member of Derived Class

, 6 Apr 2013 CPOL
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


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 :)

Firstly, a simple base class:

class Base
    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
    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>

    virtual void doSomething()
        std::cout << "DerivedOne contains " << listOfSomething.size() << " items." << std::endl;

class DerivedTwo: public DerivedTemplate<DerivedTwo>

    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.


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


About the Author

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
* Security – Cryptography, Key Management, Smart Cards
* Agile and Scrum

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    Praise Praise    Rant Rant    Admin Admin   

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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170424.1 | Last Updated 6 Apr 2013
Article Copyright 2013 by jsolutions_uk
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid