473,587 Members | 2,516 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A traversal problem

Hello

I have a design problem, which I don't seem to have a good solution. By
example:

class B;
class C;

class D
{
public:
// Something
};

class A
{
public:
void clear();
int numberOfBs() const;
private:
map<integer, B*> data_;
};

class B
{
public:
void clear();
int numberOfCs() const;
private:
map<integer, C*> data_;
};

class C
{
public:
void clear();
int numberOfDs() const;
private:
vector<D> data_;
};

The similar interfaces is just a coincidence. As you can see the classes
form a hierarchy. This hierarchy is only finite, that is, I am not after
something like composite pattern.

The class hierarchy is made to partition the D data into chunks which
are more conveniently used in the program.

Now, the data in the Ds is the one that should be modifiable by a user.
The user should not be able to use the non-const functions in B and C
(such as clear). The user should only be able to modify the data in Ds
and use the const functions.

It is like the interfaces should be partitioned into two: one for the
"administra tor" and one for the "user". On the other hand, I think
iterators must be used in any case, but how do you satisfy the non-const
requirements?

I hope this description is not too vague.

So how do you traverse all D's in all C's in all B's in all A's with
these requirements?

--
Kalle Rutanen
http://kaba.hilvi.org
Mar 5 '06 #1
3 1601
* Kaba:
So how do you traverse all D's in all C's in all B's in all A's with
these requirements?


The requirements seem to be that C, B and A should be non-modifyable
from the client code.

Then expose interfaces, not C, B and A themselves.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 5 '06 #2
Kaba wrote:
I have a design problem, which I don't seem to have a good solution.
By example:

class B;
class C;

class D
{
public:
// Something
};

class A
{
public:
void clear();
int numberOfBs() const;
private:
map<integer, B*> data_;
};

class B
{
public:
void clear();
int numberOfCs() const;
private:
map<integer, C*> data_;
};

class C
{
public:
void clear();
int numberOfDs() const;
private:
vector<D> data_;
};

The similar interfaces is just a coincidence. As you can see the
classes form a hierarchy. This hierarchy is only finite, that is, I
am not after something like composite pattern.

The class hierarchy is made to partition the D data into chunks which
are more conveniently used in the program.

Now, the data in the Ds is the one that should be modifiable by a
user. The user should not be able to use the non-const functions in B
and C (such as clear). The user should only be able to modify the
data in Ds and use the const functions.
So, why aren't those functions that the user isn't supposed to use
declared 'private' then?
It is like the interfaces should be partitioned into two: one for the
"administra tor" and one for the "user".
There are several solutions for this. You can declare the 'adminstrator'
a friend of those classes. You can provide a derived class for all the
administrative needs and keep the administration interface 'protected'...
On the other hand, I think
iterators must be used in any case, but how do you satisfy the
non-const requirements?
Make your iterators 'const' up until when they dereference to 'D&'.
I hope this description is not too vague.
Uh.. Maybe just a bit.
So how do you traverse all D's in all C's in all B's in all A's with
these requirements?


I would probably write a bunch of 'for_each' member functions.

You didn't specify what your user gets to work with. What does he/she
have? An instance of 'A'?

V
--
Please remove capital As from my address when replying by mail
Mar 6 '06 #3
This is to both Alf and Victor. Thanks for the replies.

Alf: A is modifyable, while B and C accessed through A are not. The user
is given A. I didn't quite get your answer of exposing interfaces.. Did
you mean to encapsulate B and C totally inside A?
Now, the data in the Ds is the one that should be modifiable by a
user. The user should not be able to use the non-const functions in B
and C (such as clear). The user should only be able to modify the
data in Ds and use the const functions.
So, why aren't those functions that the user isn't supposed to use
declared 'private' then?


It is because the classes themselves, each A, B, C and D should also be
useable by themselves.
It is like the interfaces should be partitioned into two: one for the
"administra tor" and one for the "user".


There are several solutions for this. You can declare the 'adminstrator'
a friend of those classes. You can provide a derived class for all the
administrative needs and keep the administration interface 'protected'...


Yes, I also thought of this.. But then I would have to lose the property
that the classes were usable by themselves..
On the other hand, I think
iterators must be used in any case, but how do you satisfy the
non-const requirements?


Make your iterators 'const' up until when they dereference to 'D&'.


This protects the pointers from modification, but when you get the
pointer to B, for example, you are again free to modify it as you wish.
On the other hand, if in some way during the chain from A to D the
object is const, then D is also and can't be modified.
I hope this description is not too vague.
Uh.. Maybe just a bit.


Probably a lot, I am sorry:)
I would probably write a bunch of 'for_each' member functions.

You didn't specify what your user gets to work with. What does he/she
have? An instance of 'A'?


Yes, the user gets the A.

Now, I know the problem description is not enough. So I try again with
my actual application.

I am programming a music composing programming. The top class A is
called Tune. This contains the instruments and notes etc.

The note information in Tune (A) is a collection of Patterns.
A Pattern (B) is a collection of Channels.
A Channel (C) is a collection notes (D).

A Channel is similar to a staff. A Pattern is a collection of parallel
staffs (for example, piano and bass can play at the same time) of some
finite time, kind of blocks. The Tune is then a collection of these
blocks.

The time in a Channel is discretely divided into pieces. The user (well,
a programmer using the api), should be able to directly write and read
these time pieces (write note there, clear that note, etc..).

But the user should also have access to the larger pieces, such as
Patterns, so he can read something useful from it. As the Patterns and
Channels are organized by Tune, the user should not have modification
access to them.

--
Kalle Rutanen
http://kaba.hilvi.org
Mar 6 '06 #4

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

Similar topics

16
2353
by: Tim Tyler | last post by:
Today's: "Directory Traversal Vulnerability": - http://secunia.com/advisories/10955/ More evidence tht PHP was hacked together rapidly without a great deal of thought being given to security. -- __________ |im |yler http://timtyler.org/ tim@tt1lock.org Remove lock to reply.
2
4603
by: ravi mannan | last post by:
Hello all, I'm trying to read an xml file and create a nested JPopupMenu from that. The first thing I want to do is to read in the xml file and put it in a Document using DOM and then do a post-order traversal of the DOM tree. This will let me start at the bottom of the tree, which will be the deepest selections in the menu, and add the...
1
2044
by: guy001 | last post by:
Hi, I'm trying to traverse the DOM in a bit of a non-traditional manner and am struggling to get my head around it. Just say i have some elements like so: A |-B |-C | |-D |
2
2789
by: FrankEBailey | last post by:
I've been reading up on Modified Preorder Tree Traversal and it's definitely ideal for the kind of tree structures I need to model for my company's multi-level sales hierarchy. I've implemented the database side already in SQL Server 2000 and can retrieve all child nodes based on the left and right IDs of the current node. My problem is...
1
3421
by: Patient Guy | last post by:
I am trying to write functions that manipulate objects representing dynamic tables in HTML documents in a more friendly way (to me). I do this by creating a table object (tableObject) through a constructor (code below) whose properties include .rows and .columns and arrays corresponding to cells, and I can just set/get values (probably...
2
3788
by: xandra | last post by:
i have 2 question in breadth-first traversal.your help will be appreciated. 1. what is the purpose of the queue in breath-first traversal? 2 suppose you had a function call displayAtDepthN which when given a tree and depth would display only the nodes at that depth. explain how this could be used to give a breadth-first traversal of the...
6
4062
by: GrispernMix | last post by:
//ques and and level order traversal file name: lab6_build_leaf_up.cpp Instructions:
0
1316
by: Ravi Kumar | last post by:
hi :) I was trying to develop a custom mod_python based web-site, just today. the problem I got though i liked the mod_python's feature of mapping and calling functions in python script by parsing the url. I mean, http://localhost/site/member/list?no=100 would call site/member.py page's function list with arguments no=100. Thats a feature...
6
20008
by: APEJMAN | last post by:
would you please help me? I wrote 3 separate line of code for printing my binary tree, and now I am trying to print the level-order traversal of the tree, where the nodes at each level of the tree are printed on a separate line. my codes are below,( for printing inorder, preorder and post order) I have no Idea how I can print them in ,...
2
3803
by: slizorn | last post by:
hi guys, i need to make a tree traversal algorithm that would help me search the tree.. basically i need to read in a text file... shown below H H,E,L E,B,F B,A,C A,null,null c,null,D
0
8216
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7974
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6629
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5719
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5395
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3882
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2364
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1455
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1192
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.