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

A simple c++ question...

I have a few classes...

class a : std::list<baseclass>
{
int keya;
};

class b : std::list<a>
{
int keyb;
};

class c : std::list<b>
{
int keyc;
};
I'm trying to figure out at each level the size of the lists...

so if I have:

int main(int argc, char *argv[])
{
c x;

cout << "The size of the c class list is " << x.size() << endl;
cout << "The size of the b class list is " << ???? << endl;
cout << "The size of the a class list is " << ???? <<< endl;
}
-------------------------------------------------------------------------

Thinking about it...
Using inheritance of std::list may not have been the best idea..
perhaps:

class a : std::list<baseclass>
{
int keya;
std::list<sometype> baseclasslist;
};

class b
{
int keyb;
std::list<a> alist;
};

class c
{
int keyc;
std::list<b> blist;
};

May have been a better implementation, but the question remains.

Jul 22 '05 #1
10 1822
"JustSomeGuy" <no**@nottelling.com> wrote...
I have a few classes...
[..]


Patience is a virtue
Jul 22 '05 #2
Your problem was you were inherting privately, so you couldn't
access the size() function in the superclass of a, b,c etc. Here's
a working piece of code to 'splain it.

davet.

#include <iostream>
#include <list>
using namespace std;
class baseclass
{
public:

};
class A : public std::list<int>
{

int keya;
};

class B : public std::list<A>
{

int keyb;
};

class C : public std::list<B>
{

int keyc;
};

int main()
{
A a;
B b;
C c;

for (int k=0; k<10; k++)
{
for (int j=0; j<10 ; j++)
{
for (int i=0; i< 10; i++)
{

a.push_back(i);

}

b.push_back(a);
}

c.push_back(b);
}

cout << "The size of the a class list is " << a.size() << endl;

cout << "The size of the b class list is " << b.size() << endl;
cout << "The size of the c class list is " << c.size() << endl;

return 0;
}

"JustSomeGuy" <no**@nottelling.com> wrote in message
news:pxFKc.54262$Mr4.2978@pd7tw1no...
I have a few classes...

class a : std::list<baseclass>
{
int keya;
};

class b : std::list<a>
{
int keyb;
};

class c : std::list<b>
{
int keyc;
};
I'm trying to figure out at each level the size of the lists...

so if I have:

int main(int argc, char *argv[])
{
c x;

cout << "The size of the c class list is " << x.size() << endl;
cout << "The size of the b class list is " << ???? << endl;
cout << "The size of the a class list is " << ???? <<< endl;
}
-------------------------------------------------------------------------

Thinking about it...
Using inheritance of std::list may not have been the best idea..
perhaps:

class a : std::list<baseclass>
{
int keya;
std::list<sometype> baseclasslist;
};

class b
{
int keyb;
std::list<a> alist;
};

class c
{
int keyc;
std::list<b> blist;
};

May have been a better implementation, but the question remains.

Jul 22 '05 #3
JustSomeGuy wrote:
I have a few classes...

class a : std::list<baseclass>
{
int keya;
};

class b : std::list<a>
{
int keyb;
};

class c : std::list<b>
{
int keyc;
};
I'm trying to figure out at each level the size of the lists...

so if I have:

int main(int argc, char *argv[])
{
c x;

cout << "The size of the c class list is " << x.size() << endl;
cout << "The size of the b class list is " << ???? << endl;
cout << "The size of the a class list is " << ???? <<< endl;
}
There is not one list for b and a, there are multiple lists.

Are you sure you want lists of lists of lists ?

-------------------------------------------------------------------------

Thinking about it...
Using inheritance of std::list may not have been the best idea..
perhaps:

class a : std::list<baseclass>
{
int keya;
std::list<sometype> baseclasslist;
};

class b
{
int keyb;
std::list<a> alist;
};

class c
{
int keyc;
std::list<b> blist;
};

May have been a better implementation, but the question remains.


It depends.
Jul 22 '05 #4
"JustSomeGuy" <no**@nottelling.com> wrote in message
news:pxFKc.54262$Mr4.2978@pd7tw1no...
I have a few classes...

class a : std::list<baseclass>
{
int keya;
};

class b : std::list<a>
{
int keyb;
};

class c : std::list<b>
{
int keyc;
};
I'm trying to figure out at each level the size of the lists...

so if I have:

int main(int argc, char *argv[])
{
c x;

cout << "The size of the c class list is " << x.size() << endl;
cout << "The size of the b class list is " << ???? << endl;
cout << "The size of the a class list is " << ???? <<< endl;
}
Each instance of c contains multiple instances (a std::list) of b, and each
instance of b contains multiple instances (another std::list) of a. Your
code implies that for each instance of c there can only be one instance of b
and only one instance of a, which is not the case.

To display the sizes of each std::list instantiated by the above code, you
would have to iterate over each list. Example:

c x;

std::cout << "The size of c class list is " << x.size() << std::endl;

for ( c::iterator cit = x.begin(); cit != x.end(); ++cit ) {
std::cout << "The size of b class list is " << cit->size() << std::endl;

for ( b::iterator bit = cit->begin(); bit != cit->end(); ++bit ) {
std::cout << "The size of a class list is " << bit->size() <<
std::endl;

for ( a::iterator ait = bit->begin(); ait != bit->end(); ++ait ) {
std::cout << "The size of baseclass class list is " <<
ait->size() << std::endl;
}
}
}

Some indentation might make the output a little clearer.
Thinking about it...
Using inheritance of std::list may not have been the best idea..

Many people frown upon inheriting from the standard containers because they
were not designed for inheritance (no virtual destructor, no virtual
functions, no protected members). I happen to be one of those people.
Composition would probably be better.

--
David Hilsee
Jul 22 '05 #5
using namespace std;

class a : public list<baseclass>
{
int keya;
};

class b : public list<a>
{
int keyb;
};

class c : public list<b>
{
int keyc;
};

void c::cleanItUp(c & x)
{
cout << "The size of c class list is " << x.size() << endl;

for ( c::iterator cit = x.begin(); cit != x.end(); ++cit )
{
cout << "The size of b class list is " << cit->size() << endl;

for ( b::iterator bit = cit->begin(); bit != cit->end(); ++bit )
{
cout << "The size of a class list is " << bit->size() << endl;

for ( a::iterator ait = bit->begin(); ait != bit->end(); ++ait )
{
cout << "The size of baseclass class list is " <<
ait->size() << endl;
}
}
}
}

At some point one instance of the baseclass is added to the list...
However for this to occure then one instance of a and one instance of b must
also exist.
This is done by the c::add(baseclass & x) method.

My question is... if that instance is removed then the parents need to be
removed as well.
So how do you check for this? ie a parent without children should remove
itself?

Jul 22 '05 #6

"JustSomeGuy" <no**@nottelling.com> wrote in message
news:KaHKc.54440$Mr4.16678@pd7tw1no...
using namespace std;

class a : public list<baseclass>
{
int keya;
};

class b : public list<a>
{
int keyb;
};

class c : public list<b>
{
int keyc;
};

void c::cleanItUp(c & x)
{
cout << "The size of c class list is " << x.size() << endl;

for ( c::iterator cit = x.begin(); cit != x.end(); ++cit )
{
cout << "The size of b class list is " << cit->size() << endl;

for ( b::iterator bit = cit->begin(); bit != cit->end(); ++bit )
{
cout << "The size of a class list is " << bit->size() << endl;

for ( a::iterator ait = bit->begin(); ait != bit->end(); ++ait ) {
cout << "The size of baseclass class list is " <<
ait->size() << endl;
}
}
}
}

At some point one instance of the baseclass is added to the list...
However for this to occure then one instance of a and one instance of b must also exist.
This is done by the c::add(baseclass & x) method.

My question is... if that instance is removed then the parents need to be
removed as well.
So how do you check for this? ie a parent without children should remove
itself?


The code you have provided is incomplete, but I'll tell you what I can.
Given the current implementation of the c class, it is impossible for it to
know when an instance of b, a, or baseclass has been added or removed from
its ancestor. Some other code could call std::list::push_back(),
std::list::erase(), std::list::clear(), etc, and the c class would have its
members modified without its knowledge or consent. The standard containers
do not provide any way for derived clases to be notified of modifications.
If you wish to control access to the std::list members of c, b, and a, then
you may want to consider making those std::lists private members of the
appropriate class and only exposing a very limited interface to the users of
the c class. That way, it may be easier to check for parents without
children, as well as guarantee other conditions that cannot be guaranteed by
the current implementation. In that scenario, it would be easy for a member
function c::add(baseclass & x), could have a member function
c::remove(<something>) to "undo" that operation. That is just a guess. I'd
have to know more about what you are doing to give you a complete answer.

--
David Hilsee
Jul 22 '05 #7

"David Hilsee" <da*************@yahoo.com> wrote in message
news:3a********************@comcast.com...

"JustSomeGuy" <no**@nottelling.com> wrote in message
news:KaHKc.54440$Mr4.16678@pd7tw1no...
using namespace std;

class a : public list<baseclass>
{
int keya;
};

class b : public list<a>
{
int keyb;
};

class c : public list<b>
{
int keyc;
};

void c::cleanItUp(c & x)
{
cout << "The size of c class list is " << x.size() << endl;

for ( c::iterator cit = x.begin(); cit != x.end(); ++cit )
{
cout << "The size of b class list is " << cit->size() << endl;

for ( b::iterator bit = cit->begin(); bit != cit->end(); ++bit )
{
cout << "The size of a class list is " << bit->size() << endl;
for ( a::iterator ait = bit->begin(); ait != bit->end(); ++ait )
{
cout << "The size of baseclass class list is " <<
ait->size() << endl;
}
}
}
}

At some point one instance of the baseclass is added to the list...
However for this to occure then one instance of a and one instance of b

must
also exist.
This is done by the c::add(baseclass & x) method.

My question is... if that instance is removed then the parents need to be removed as well.
So how do you check for this? ie a parent without children should remove itself?


The code you have provided is incomplete, but I'll tell you what I can.
Given the current implementation of the c class, it is impossible for it

to know when an instance of b, a, or baseclass has been added or removed from
its ancestor. Some other code could call std::list::push_back(),
std::list::erase(), std::list::clear(), etc, and the c class would have its members modified without its knowledge or consent. The standard containers do not provide any way for derived clases to be notified of modifications.
If you wish to control access to the std::list members of c, b, and a, then you may want to consider making those std::lists private members of the
appropriate class and only exposing a very limited interface to the users of the c class. That way, it may be easier to check for parents without
children, as well as guarantee other conditions that cannot be guaranteed by the current implementation. In that scenario, it would be easy for a member function c::add(baseclass & x), could have a member function
c::remove(<something>) to "undo" that operation. That is just a guess. I'd have to know more about what you are doing to give you a complete answer.

--
David Hilsee


I guess one way of looking at it would be to treat it like a directory
structure.

If a directory exists but has no files then that directory should be
deleted.

If a directory exists with no subdirectories or files then it too should be
deleted.

Does that explain it a bit better?

TIA
B.
Jul 22 '05 #8

"JustSomeGuy" <no**@nottelling.com> wrote in message
news:5dYKc.58241$Mr4.3985@pd7tw1no...
I guess one way of looking at it would be to treat it like a directory
structure.

If a directory exists but has no files then that directory should be
deleted.

If a directory exists with no subdirectories or files then it too should be deleted.

Does that explain it a bit better?


A little. My recommendation for making the containers private sounds right.
I don't really want to write the code out, but the approach is simple: write
classes (or possibly just one class) that contain private list(s) (or
whatever container), and then write public member functions that will add
and remove elements from those lists. The member functions can then ensure
that empty "directories" are removed.

--
David Hilsee
Jul 22 '05 #9
In message <5dYKc.58241$Mr4.3985@pd7tw1no>, JustSomeGuy
<no**@nottelling.com> writes
I guess one way of looking at it would be to treat it like a directory
structure.

If a directory exists but has no files then that directory should be
deleted.

If a directory exists with no subdirectories or files then it too should be
deleted.

I'm glad you're not running any system I have to use. The mere existence
of a directory may convey important information, regardless of whether
there's anything in it.

--
Richard Herring
Jul 22 '05 #10

"Richard Herring" <ju**@[127.0.0.1]> wrote in message
news:GX**************@baesystems.com...
In message <5dYKc.58241$Mr4.3985@pd7tw1no>, JustSomeGuy
<no**@nottelling.com> writes
I guess one way of looking at it would be to treat it like a directory
structure.

If a directory exists but has no files then that directory should be
deleted.

If a directory exists with no subdirectories or files then it too should bedeleted.
I'm glad you're not running any system I have to use. The mere existence
of a directory may convey important information, regardless of whether
there's anything in it.


This is just an example... If you read the original posting you will see
that this is a hypothetical example.
--
Richard Herring

Jul 22 '05 #11

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

Similar topics

3
by: Patchwork | last post by:
Hi Everyone, Please take a look at the following (simple and fun) program: //////////////////////////////////////////////////////////////////////////// ///////////// // Monster Munch, example...
1
by: Proteus | last post by:
Any help appreciated on a small perl project I need to write for educator/teaching purposes. I have not programmed perl for some time, need to get up to speed, maybe some kind souls hrere will help...
2
by: Raskolnikow | last post by:
Hi! I have a very simple problem with itoa() or the localtime(...). Sorry, if it is too simple, I don't have a proper example. Please have a look at the comments. struct tm *systime; time_t...
3
by: Peter | last post by:
Hello Thanks for reviewing my question. I would like to know how can I programmatically select a node Thanks in Advanc Peter
7
by: abcd | last post by:
I am trying to set up client machine and investigatging which .net components are missing to run aspx page. I have a simple aspx page which just has "hello world" printed.... When I request...
4
by: dba_222 | last post by:
Dear Experts, Ok, I hate to ask such a seemingly dumb question, but I've already spent far too much time on this. More that I would care to admit. In Sql server, how do I simply change a...
14
by: Giancarlo Berenz | last post by:
Hi: Recently i write this code: class Simple { private: int value; public: int GiveMeARandom(void);
30
by: galiorenye | last post by:
Hi, Given this code: A** ppA = new A*; A *pA = NULL; for(int i = 0; i < 10; ++i) { pA = ppA; //do something with pA
10
by: Phillip Taylor | last post by:
Hi guys, I'm looking to develop a simple web service in VB.NET but I'm having some trivial issues. In Visual Studio I create a web services project and change the asmx.vb file to this: Imports...
17
by: Chris M. Thomasson | last post by:
I use the following technique in all of my C++ projects; here is the example code with error checking omitted for brevity: _________________________________________________________________ /*...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.