473,396 Members | 2,109 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,396 software developers and data experts.

Recursive construction of derived classes.

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

Jul 23 '05 #1
1 1745
Steven T. Hatton wrote:
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[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::ostream;
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<<","<<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<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

Jul 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Jon Slaughter | last post by:
#pragma once #include <vector> class empty_class { }; template <int _I, int _J, class _element, class _property> class RDES_T {
1
by: Jon Slaughter | last post by:
I've managed to put together a template class that basicaly creates a recursive tree that lets you easily specify the "base" class of that tree and and ending notes and lets you stop the recursive...
5
by: Nathan Bullock | last post by:
Hi, I have a base class, say Base and there are two classes, say Class1 and Class2 which are derived from Base. Is there any way for me, say from a static method in Base, to get a list of all...
9
by: Larry Woods | last post by:
I have a method in my base class that I want ALL derived classes to use. But, I find that I can create a "Shadow" method in my derived class that "overrides" the method in my base class. Can't...
8
by: Manuel | last post by:
Hi! If I've a vector filled with abstract classes, can I push in it the derived classes too? Even if derived classes have new methods? I've done some experiments, and it seem I can push the...
6
by: ivan.leben | last post by:
I want to write a Mesh class using half-edges. This class uses three other classes: Vertex, HalfEdge and Face. These classes should be linked properly in the process of building up the mesh by...
7
by: BeautifulMind | last post by:
In case of inheritence the order of execution of constructors is in the order of derivation and order of destructor execution is in reverse order of derivation. Is this case also true in case...
9
by: desktop | last post by:
On this page: http://www.eptacom.net/pubblicazioni/pub_eng/mdisp.html Shape specify the virtual function: virtual double Intersect( const Shape& s) = 0; then the derived class Circle...
15
by: Bob Johnson | last post by:
I have a base class that must have a member variable populated by, and only by, derived classes. It appears that if I declare the variable as "internal protected" then the base class *can*...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.