Inheritance with specialized members

N

Nicola

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(Node* n) { adjList->push_back(n); }
Node* getChild(int i) const { return adjList->at(i); }
// etc...

protected:
vector<Node*>* adjList;
Data* data;
};


class GraphBase {
protected:
GraphBase() { adjList = new vector<Node*>(); }
~GraphBase() { /* Free adjacency list */ }
void addNode(Data* d) { adjList->push_back(new Node(d)); }
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:
vector<Node*>* adjList;
int currentNode;
};

template <class NodeType>
class Graph : private GraphBase {
public:
Graph() : GraphBase() { }
~Graph;
void addNode(Data* d) { GraphBase::addNode(d); }
void addEdge(Node* s, Node* t) { GraphBase::addEdge(s, t); }
NodeType* getNode(int i) const { return (NodeType*)GraphBase::getNode(
i); }
NodeType* getFirstNode() const;
NodeType* getNextNode() const;
/* etc... */
};

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

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

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*)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?

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

Thanks in advance for your attention and for any advice you will be so
kind to give me.

Nicola
 
J

John Isaacks

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:public INode
{
virtual foo(void) { ... };
virtual bar(void) { ... };
};

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

You will not have to type cast anything
SpecialNode* foo() { return (SpecialNode*)adjList->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(void) = 0;
virtual void DoSomething1(void) = 0;
static INode* CreateInstance(void) = 0;
static INode* CreateAltInstance(void) = 0
};

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

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

INode* INode::CreateAltInstance(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,
add new virtual routines
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,787
Messages
2,569,629
Members
45,330
Latest member
AlvaStingl

Latest Threads

Top