Recursive construction of derived classes.

S

Steven T. Hatton

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: <[email protected]>
"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::eek:stream;
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<<","<<depth<<")" << 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
 
S

Steven T. Hatton

Steven said:
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:
<[email protected]> "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[now] 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?

OK, it looks like I solved to problem with one minor complication. The
stupid compliler can't figure out how to create a void( unsigned,
unsigned). I now have no way of using Base<Derived_T> without haveing a
class that will satisfy the call to Node(more - 1, depth + 1); I tried
forward declaring template<Derived_T> class Base;, but that resulted in
syntax errors. Suggestions?

#include <iostream>
#include <vector>
#include <string>
#include <iomanip>

using std::cout;
using std::cerr;
using std::endl;
using std::setw;
using std::eek:stream;
using std::string;

template<typename 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<<","<<depth<<")" << 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<Derived_T> {
public:
Base(unsigned more, unsigned depth=0)
:Root<Derived_T>(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
 

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,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top