473,549 Members | 3,113 Online

# Inheritance with specialized members

Consider the following implementation of a graph, whose nodes must be of
type Node or of a subclass of Node:

class Node {
public:
Node(Data* d) { adjList = new vector<Node*>; data = d; }
virtual ~Node() { delete adjList; }
virtual void addNode(Data* d) { /*...*/ }
void connectToNode(N ode* n) { adjList->push_back(n) ; }
Node* getChild(int i) const { return adjList->at(i); }
// etc...

protected:
Data* data;
};
class GraphBase {
protected:
GraphBase() { adjList = new vector<Node*>() ; }
~GraphBase() { /* Free adjacency list */ }
void addEdge(Node* s, Node* t) { s->connectToNode( t); }
Node* getNode(int i) const { return adjList->at(i); }
Node* getFirstNode() const;
Node* getNextNode() const;
/* etc... */

private:
int currentNode;
};

template <class NodeType>
class Graph : private GraphBase {
public:
Graph() : GraphBase() { }
~Graph;
NodeType* getNode(int i) const { return (NodeType*)Grap hBase::getNode(
i); }
NodeType* getFirstNode() const;
NodeType* getNextNode() const;
/* etc... */
};

class SpecialNode : public Node { SpecialNode* foo(); /* etc... */ };

class SpecialGraph : public Graph<SpecialNo de> { /*...*/ };

I have a few questions about the previous partial code:

1) Is there a better way of implementing Node, knowing that it will be
specialized? As it is, whenever one of the methods returning a Node* is
called, there probably must be an explicit cast to the correct subclass
in the caller code, e.g. an implementation of foo() might be

SpecialNode* foo() { return (SpecialNode*)a djList->at[0]; }

2) Using a template (Graph) privately inheriting from a class (GraphBase)
is the only way I found to constrain a user of a graph to instantiate
only graphs whose nodes are of type Node or of a subtype of it. Is there
a better way to accomplish that? If I wanted to hide the GraphBase class
to the world, could I make it an inner class of the template, instead of
inheriting from it?

3) A statement such as GraphBase* g = new Graph<SpecialNo de>() is not
allowed, because the template has a private base (right?). Should I make
the destructor of GraphBase virtual anyway?

kind to give me.

Nicola
Jul 19 '05 #1
1 2095
1) Is there a better way of implementing Node, knowing that it will be
specialized? As it is, whenever one of the methods returning a Node* is
called, there probably must be an explicit cast to the correct subclass
in the caller code, e.g. an implementation of foo() might be
I prefer virtual base classes or interface classes for this.

class INode {
public:
virtual ~INode(void) { };
virtual foo(void) = 0;
virtual bar(void) = 0;
... etc ... to hand all types of routine
};

class SpecialNode1:pu blic INode
{
virtual foo(void) { ... };
virtual bar(void) { ... };
};

class SpecialNode2:pu blic INode
{
virtual foo(void) { ... };
virtual bar(void) { ... };
};

You will not have to type cast anything

SpecialNode* foo() { return (SpecialNode*)a djList->at[0]; }
the above becomes...
INode* foo() { return adjList->at[0]; };

2) Using a template (Graph) privately inheriting from a class (GraphBase)
is the only way I found to constrain a user of a graph to instantiate
only graphs whose nodes are of type Node or of a subtype of it. Is there
a better way to accomplish that? If I wanted to hide the GraphBase class
to the world, could I make it an inner class of the template, instead of
inheriting from it?

if you use pure virtual base classes or sometimes refered to interface
classes, you can prevent the
user from doing anything. You can force them to only create the type of
objects you want.
and if you put everything into a DLL you can even hide the implementation
from the user.
All you give them is the interface class.

// example .h
// given to users of Node
class INode {
protected:
// restrict user from new/delete, must use CreateInstance( ) and Release()
INode(void);
virtual ~INode(void);
public
virtual void Release(void) = 0;
virtual void DoSomething1(vo id) = 0;
virtual void DoSomething1(vo id) = 0;
static INode* CreateInstance( void) = 0;
static INode* CreateAltInstan ce(void) = 0
};

// example of .h for DLL use only
class SpecialNode1 : public INode
{
.....
};

// example of .cpp for DLL
#include "INode.h"
#include "SpecialNod e.h"
#include "AltSpecialNode .h"
INode* INode::CreateIn stance(void) {
return new SpecialNode;
};

INode* INode::CreateAl tInstance(void) {
return new AltSpecialNode;
};
The user only sees the INode class.

If you never change your interface class all old programs linked to an old
DLL will
still work with a newer revised DLL. If you have to change the interface,
to the end ( never delete routines or change how and an old routine is
called ).
You will end up with a dll that works with your old programs as well as your
new ones without being recompiled.

Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.