is friend the only way ?

We are class-a which has "map container" of object-p. Now object-b has a shared ptr of class-a, and should create dns objects for every p-object in map (present in class a).

class p {

protected :
ipaddress ip;
bool flag;
};
typedef sharedptr of p is pptr;

class a {

protected:
map <ipaddress, p*> pmap;
}
typedef sharedptr of a is aptr;

class b {
public :
start() {
for all p in a_ {
create dns and insert in dns_map
}
protected :
aptr a_;
map <int, dns *> dns_map
};

class dns {
public :
dns ( sharedptr of p) {
p_ = p;
}

protected :
pptr p_;
}


My question is, should i have to make class-b a friend class of class-a for
class b->start() to access the map container in class-a to create dns objects?
There will be one dns object per p object element in class a->pmap.
or is there any other option to do this? plz advice

Please let me know if its not clear will try to rephrase.
Last edited on
Other options off the top of my head.

You could create a::begin() and a::end() functions that return iterators to the map. This would make the iterators accessible to all.

You could have a function in a which takes a dns_map and puts all its entries into it.

By the way, I hope you have better class names than a, b and p. It's difficult to figure out what's going on with cryptic names like those.
Thanks Doug. Below is the exact context I am talking about.

We have "class office" which has mulitple systems. Now we need to have a class
"trunk" (has shared ptr of office) for every office. This trunk object should
have an channel created for every system in the office.

class system {

protected :
ipaddress ip;
bool flag;
};
typedef sharedptr of system is systemptr;

class office {

protected:
map <ipaddress, system*> systemmap;
}
typedef sharedptr of office is officeptr;

class trunk {
public :
start() {
for all system in office_ {
create channel and insert in channel_map
}
protected :
officeptr office_;
map <int, channel *> channel_map
};

class channel {
public :
channel ( sharedptr of system) {
system_ = system;
}

protected :
systemptr system_;
}


My question is, should i have to make "trunk" a friend class of "
office" for trunk::start() to access the container system_map in "office"
to create "channel" objects per every system?
or is there any other option to do this? plz advice

Please let me know if its not clear will try to rephrase.
By the way, please use code tags when you post code. Use the button that shows "<>" to create the tags, and then post your code between them. It makes it easier to read and refer to your code.

Now, on to your code...

Who calls start()? If office calls start(), I would pass the systemmap as a reference argument to the function call. If start() is called from the outside, put a function in office that takes channel_map as an argument to insert the info.
Looks like I missed couple of classes. I have now elaborated and this is the exact set of classes that I have.
<
--------------------
class system {

protected :
ipaddress ip;
bool flag;
};
typedef sharedptr of system is sysptr;

********
class Remoteoffice {

protected:
map <ipaddress, system*> systemmap;
private:
set<trunkptr> trunks_created;
}
typedef sharedptr of office is remptr;

********
class RemoteofficeManager {

create_Remoteoffice (office_id, ipaddr[], numb_sys nsys);
delete_remoteoffice (office_id);

protected :
map < office_id, Remoteoffice *> remmap;
}

------------------------------
class Gw {

protected:
ipaddress gw;
private:
set<trunkptr> trunks_created;
}
typedef sharedptr of office is Gwptr;

********
class GwManager {
add_gw ();

del_gw();

protected :
map <ipaddress, Gw*> Gwmap;
}

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

class trunk {

protected :
RemoteOfficeptr rem_;
Gwptr gw_;

map <int, channel *> channel_map
};
typedef sharedptr of office is trkptr;

***********
class channel {
public :
channel ( sharedptr of system) {
system_ = system;
}

protected :
systemptr system_;
}

**********
class TrunkMgr {
add_trnk();
del_trnk();

protected :
map <int, trunk *> trunk_map
}

>


Question - 1
Now when a Gw is created it has to create trunks one for each office and each trunk should have a channel for each system.

1) GWManager is called to create a GW ->add_gw()
2) Now We need to iterate through all the Remote offices in (Remoteoffice
Manager, remmap) and try to create trunks (and internally channels for each
system in the remote office).

So for this GWManager::add_gw() needs to iterate through RemoteOffice:remmap.
What is the best way to do this?
1) Should RemoteOffice expose the iterators of the remmap? so that GWManager
2) or through a function that takes a map? plz let me know the function
prototype for this.

Question-2:

While Deleting the GW. It has to delete all the trunks first.
- GW::del_gw() will iterate through trunks_created (set container of shared_ptrs) and will have to delete the trunks.

Now the question is - I am walking through a set of shared ptrs of trunk and trying to delete individual trunks. Will this corrupt the iterator?


Last edited on
By the way, please use code tags when you post code. Use the button that shows "<>" to create the tags, and then post your code between them. It makes it easier to read and refer to your code.

Question 1. If I understand what you are doing, I would add the function addGateway to the RemoteOfficeManager, taking the newly created gateway as an argument. This function would loop through its RemoteOffices and add a new trunk from the gateway to each office. Functions will be needed in RemoteOffice and Gw to add the new trunks to their sets.

Question 2. See "Iterator validity" at http://www.cplusplus.com/reference/set/set/erase/



Thank you Doug.
Couple of tables of peers and they need to be synced, plz let me know how do I rearrange them.

1) There are multiple remote offices and each remote office has multiple systems.
I have the below data structures for these.

office_map(office_id, sharedptr <office>)
remote_system_map(priv_ipaddr, address_details *)


2) There is another table which is a subset of the above tables(have only few systems of the above various offices) Each of the private ipaddress has a list of associated special ipaddress.

{
private ipaddr
- list (special_ipaddr/l4_port)
- address_details *
}

special_peer_table should support
- Lookup() using private ipaddress
Not sure what container I should be using for this special_peer_table



Topic archived. No new replies allowed.