I started working on something I had failed to understand several months
ago. The original discussion on c.l.c++-m has this message ID:
news://Message-ID: <Ps*******************@newsread2.news.atl.earthlin k.net>
"A recurring problem with inheritance"
I thought I had understood what the code was doing, just not how it was
doing it. I've not come to understand that I understood neither. The
original code from Dag Viken appeared to me to be creating a three-deep
derivation when, in fact, it was only creating two two-deep classes.
I've not given up trying to figure out how to produce an n-deep hierarchy
just yet, but, since this seems like an interesting problem, I figured I'd
ask if others see a way to accomplish that. As the following code stands,
the class "Derived" kicks off the recursion, but does not participate in
it, thereafter. The objective is to have a linked list of
Root<>::Base<>::Derived objects. Does anybody see a way to accomplish
that?
#include <iostream>
#include <vector>
#include <string>
#include <iomanip>
using std::cout;
using std::cerr;
using std::endl;
using std::setw;
using std::ostream;
using std::string;
template<class Node>
class Root {
protected:
Root * next;
unsigned _count;
unsigned _depth;
public:
Root(unsigned more, unsigned depth=0)
:next(0)
,_count(more)
,_depth(depth)
{
cerr<<setw(2*_depth)<<""<<"Root("<<more<<","<<dept h<<")" << endl;
if(more) next = new Node(more - 1, depth + 1);
}
virtual ~Root() {
delete next;
cerr << setw(2*_depth) << "" << "~Root: #" << _depth << endl;
}
};
template<typename Derived_T>
class Base: public Root<Base<Derived_T> > {
public:
Base(unsigned more, unsigned depth=0)
:Root<Base>(more, depth)
{
cerr<<setw(2 * depth)<<""<<"Base("<< more<<","<<depth<< ")" << endl;
};
virtual ~Base() { cerr<<setw(2 * _depth)<<""<<"~Base: #"<<_depth<< endl; }
};
class Derived: public Base<Derived> {
public:
Derived(unsigned more, unsigned depth=0)
:Base<Derived>(more, depth)
{
cerr<<setw(2 * depth)<<""<<"Derived("<<more<<","<<depth<< ")" << endl;
}
virtual ~Derived() { cerr<<setw(2*_depth)<<""<<"~Derived:
#"<<_depth<<endl; }
};
void base(){
cout << "\nVVVVVVVVVV\n";
cout << "root\n";
Base<void> b(5);
cout << "^^^^^^^^^^^^^^^^\n";
}
void derived(){
cout << "\nVVVVVVVVVV\n";
cout << "derived\n";
Derived d(5);
cout << "^^^^^^^^^^^^^^^^\n";
}
int main(int argc, char* argv[]){
base();
derived();
}
--
"If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true." - Bertrand
Russell