471,613 Members | 2,249 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,613 software developers and data experts.

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(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
Jul 19 '05 #1
1 1977
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.


Jul 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Asapi | last post: by
14 posts views Thread by Steve Jorgensen | last post: by
3 posts views Thread by Aaron Watters | last post: by
26 posts views Thread by Michael A. Covington | last post: by
6 posts views Thread by burningodzilla | last post: by
12 posts views Thread by Massimo | last post: by
11 posts views Thread by John | last post: by
3 posts views Thread by Jess | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.