Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Typed Iteration over a Composite

, 13 Oct 2004
Implementation of STL compliant type sensitive composite iterators.
#pragma once

#include "GenericTree.h"
#include "Visitor.h"

#include <string>

class CNodeBase :
	public Loki::BaseVisitable<>,
	public Types::CGenericTree<CNodeBase>
	CNodeBase(void) : m_id( m_nextId++ ) {}
	CNodeBase( const CNodeBase &src ) : Types::CGenericTree<CNodeBase>( src ), m_id( m_nextId++ ) {}
	virtual ~CNodeBase(void) {}
	CNodeBase &operator=( const CNodeBase &rhs ) { if ( this != &rhs ) Types::CGenericTree<CNodeBase>::operator=( rhs ); return *this; }
	virtual CNodeBase *Clone() { return new CNodeBase( *this ); }

	unsigned long GetId() const { return m_id; }
	virtual std::string GetType() const { return std::string( "CNodeBase" ); }

	unsigned long m_id;
	static unsigned long m_nextId;

class CNodeDerived1 :
	public CNodeBase
	CNodeDerived1(void) {}
	CNodeDerived1( const CNodeDerived1 &src ) : CNodeBase( src ) {}
	virtual ~CNodeDerived1(void) {}
	CNodeDerived1 &operator=( const CNodeDerived1 &rhs ) { if ( this != &rhs ) CNodeBase::operator=( rhs ); return *this; }
	virtual CNodeDerived1 *Clone() { return new CNodeDerived1( *this ); }

	virtual std::string GetType() const { return std::string( "CNodeDerived1" ); }


class CNodeDerived2 :
	public CNodeBase
	CNodeDerived2(void) {}
	CNodeDerived2( const CNodeDerived2 &src ) : CNodeBase( src ) {}
	virtual ~CNodeDerived2(void) {}
	CNodeDerived2 &operator=( const CNodeDerived2 &rhs ) { if ( this != &rhs ) CNodeBase::operator=( rhs ); return *this; }
	virtual CNodeDerived2 *Clone() { return new CNodeDerived2( *this ); }

	virtual std::string GetType() const { return std::string( "CNodeDerived2" ); }


unsigned long CNodeBase::m_nextId = 0;

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Dave Handley
Web Developer
United Kingdom United Kingdom
I started programming on 8 bit machines as a teenager, writing my first compiled programming language before I was 16. I went on to study Engineering and Computer Science at Oxford University, getting a first and the University Prize for the best results in Computer Science. Since then I have worked in a variety of roles, involving systems management and development management on a wide variety of platforms. Now I manage a software development company producing CAD software for Windows using C++.
My 3 favourite reference books are: Design Patterns, Gamma et al; The C++ Standard Library, Josuttis; and Computer Graphics, Foley et al.
Outside computers, I am also the drummer in a band, The Unbelievers and we have just released our first album. I am a pretty good juggler and close up magician, and in my more insane past, I have cycled from Spain to Eastern Turkey, and cycled across the Namib desert.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141223.1 | Last Updated 14 Oct 2004
Article Copyright 2004 by Dave Handley
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid