I have become completely fried looking at this, but have two main questions about the following code:
- Do I risk accessing "dead memory" by passing parameters by reference into addNeighbors() ?
(this class will be kept in a map<int, Node> object inside a Network class)
I very much want to do this by reference for:
*speed
*memory efficiency
*access to neighbor Nodes
- What am I doing wrong when calling getNeighbors()?
(last lines of code)
int addNeighbor(const Node &newNode) if you use a reference to a const your code will work also for const objects. As it is now code like this doesn't work: n0.addNeighbor(Node(3));
You are only calling addNeighbor on n0 so I don't see how n1 could have a neighbour.
You can't store references in any container. If you don't want to store copies you should store pointers.
I think I have most of it working now, using boost shared_ptr.
Unfortunately, now I am still having trouble with find()
**Can you please advise on how to make isNeighbor() work?**
So, the last 2 lines seem incorrect.
---
Output of code below is:
0 adding neighbor 1
0 adding neighbor 2
1 adding neighbor 0
2 adding neighbor 1
0's neighbors
1 has 1 neighbors
0
2 has 1 neighbors
1
NOT WORKING
is 1 a neighbor of 0? 0
is 0 a neighbor of 1? 0
Note that it's bad design to have the ownership of each node shared equally by all of its neighbors: this simple example has a memory leak because there are cycles of shared pointers pointing at each other.
I would rather have a master container of nodes (possibly of shared pointers to nodes), and each node would hold a set of non-owning raw (possibly weak) pointers to its neighbors, or perhaps a set of IDs of its neighbors, or some other way of quickly uniquely identifying them.
Hm, one problem is that STL containers can't contain standard C++ pointers, since they don't meet the requirements of STL container objects (copyable, etc.).
I have been looking for a solution that lets each node have access to the memory locations of its neighbors, but using the STL for convenience. This has been surprisingly difficult.
If I store a vector of neighbor IDs as integers in each node, I break the object-oriented model a little, and waste memory, so pointers would be very nice if they can be made to work properly.
Hm, one problem is that STL containers can't contain standard C++ pointers, since they don't meet the requirements of STL container objects (copyable, etc.).
I believe STL containers can contain standard C++ pointers. It is just that if you intend to use this container in some algorithms etc, you have to do extra work as the elements inside the container are pointers and not the actual objects themselves. You need to explicitly tell the algorithms how to do matching, comparing the objects they pointed to instead of pointers themselves etc.
This is extremely tricky but the benefit is you do save a lot of memory space and that counts when this container has many elements.
Can you give me a hint about how to do the comparisons in this case? Is this the overloading of < and == operation that I have seen in various places?
Yes. And also the copy constructor and assignment operator overloading. You just need to check which algorithm you are using with the container and then code accordingly.
If you just use it to contain and then iterate through and invoke pointer pointed to object member function, you can take easy way out and no need to do the above.
What I am saying is if your use of the container of pointers is restricted to just insert and iterate through the elements to call member function, you don't have to spend effort to do operator overloading as discussed. But if you intend to use the STL algorithm which take parameters like your container, then you need to do the proper operator overloading so the algorithm can work "properly".