Hierarchy with `list` type I want to establish a hierarchy, where at each level of the hierarchy, each type can contain a list&lt;its own type&gt;.  So Node (root of object class hierarchy) contains a list&lt;Node*&gt;, SpecialNode : Node contains a list of SpecialNode.  SpecialNode should not "accidentally" put new SpecialNode children into the list&lt;Node*&gt; in the base class, so the list&lt;&gt; are private.

I personally hate the concept of namehiding, because it is extremely prone to mistakes, uninitialized values, etc.  So here I have made it impossible (via private) for SpecialNode to "accidentally" insert into the list&lt;Node*&gt; instead of its own list&lt;SpecialNode*&gt;.

struct Node
{
private:
  list&lt;Node*&gt; children ;

protected:
  int v ;

public:
  Node( int iv ) : v(iv) { }

  virtual void print( const char* msg )
  {
    printf( "REGULAR CHILD %d in %s\n", v, msg ) ;
    for( Node *sn : Node::children )
      sn-&gt;print( "regular" ) ;

  }
} ;

struct SpecialNode : public Node
{
private:
  list&lt;SpecialNode*&gt; children ;

public:
  SpecialNode( int iv ) : Node(iv) { }
  void add( SpecialNode* child ) {
    children.push_back( child ) ;
  }

  void print( const char* msg )
  {
    printf( "SPECIAL CHILD %d in %s\n", v, msg ) ;
    for( SpecialNode *sn : children )
      sn-&gt;print( "special" ) ;

    //for( Node *sn : Node::children )
    //  sn-&gt;print( "regular" ) ;
  }

} ;

int main(int argc, const char * argv[])
{
  SpecialNode *sn = new SpecialNode( 1 ) ;

  // SpecialNodes must contain SpecialNode children.
  sn-&gt;add( new SpecialNode( 2 ) ) ;
  //sn-&gt;children.push_back( new Node( 3 ) ) ; // fails b/c Node is too basic to fit in SpecialNode container.

  sn-&gt;print( "root" ) ;

}


Is this a good way to do this, or is there a better way?
You might prefer to not use polymorphism at all if you want to make sure that your types remain unique. Consider something more like:

class Node
{
private:
    list&lt;Node*&gt; children ;
    int v ;

public:
    Node( int iv ) : v(iv) { }

    void add( Node* child )
    {
        children.push_back( child );
    }

    void print( const char* msg )
    {
        printf( "REGULAR CHILD %d in %s\n", v, msg );
        for( Node *sn : Node::children )
        {
            sn-&gt;print( "regular" );
        }
    }
};

class SpecialNode // no inheritance
{
private:
    list&lt;SpecialNode*&gt; children;
    int v;

public:
    SpecialNode( int iv ) : v(iv) {}
    void add( SpecialNode* child )
    {
        children.push_back( child ) ;
    }

    void print( const char* msg )
    {
        printf( "SPECIAL CHILD %d in %s\n", v, msg ) ;
        for( SpecialNode *sn : children )
            sn-&gt;print( "special" ) ;
    }
};

class Composite // no inheritance
{
private:
    Node nodes;
    SpecialNode specialNodes;

public:
    Composite( int iv ) : nodes(iv), specialNodes(iv)
    void addSpecial( SpecialNode* child )
    {
        specialNodes.add( child ) ;
    }
    void add( Node * child)
    {
        nodes.add(child);
    }

    void print( const char* msg )
    {
        nodes.print(msg);
        specialNodes.print(msg);
    }
};