473,320 Members | 1,916 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 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 2081
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Asapi | last post by:
When a derived class inherits from a base class, does the former inherits everything, including public/protected/private instance data, static data, and various methods(static, private/public,...
14
by: Steve Jorgensen | last post by:
Recently, I tried and did a poor job explaining an idea I've had for handling a particular case of implementation inheritance that would be easy and obvious in a fully OOP language, but is not at...
3
by: Aaron Watters | last post by:
A C# question about constructors/static methods and inheritance: Please help me make my code simpler! For fun and as an exercise I wrote somewhat classical B-tree implementation in C# which I...
26
by: Michael A. Covington | last post by:
Suppose class X has some public properties, and class Y inherits from X. Is there a way that some of the public properties of X can be private (hidden) in Y (but still usable by the methods...
60
by: Shawnk | last post by:
Some Sr. colleges and I have had an on going discussion relative to when and if C# will ever support 'true' multiple inheritance. Relevant to this, I wanted to query the C# community (the...
6
by: burningodzilla | last post by:
Hi all - I'm preparing to dive in to more complex application development using javascript, and among other things, I'm having a hard time wrapping my head around an issues regarding "inheritance"...
12
by: Massimo | last post by:
Hi to all, I'm facing a problem in a particularly complex inheritance hierarchy, and I'd like to know what the standard says about it and if my compiler is correct in what it does. I have two...
11
by: John | last post by:
Hi All, Although C# has Generics, it still does not support the generic programming paradigm. Multiple inheritance is required to support real generic programming. Here is a simple design pattern...
3
by: Jess | last post by:
Hello, I've been reading Effective C++ about multiple inheritance, but I still have a few questions. Can someone give me some help please? First, it is said that if virtual inheritance is...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.