C++ help needed
Hi
I needed some help with C++. Any ideas would be greatly appreciated. I have a bunch of objects which i create. Now, each object needs to do some computation in relation to all the other objects that i created. I wanted to know how each object can refer to the other objects. i want each object to do this computation locally by itself (- to make it a distributed, rather than me centrally computing something - directly from main() ) Thanks in advance |
Its kind of hard to say without knowing the specifics of your program design but...
Say you have objects A, B and C, and C needs to do a computation that requires data from A and B. I would make a method in C that takes the data you need from A and B by using 'get methods'. e.g PHP Code:
|
let me give you the complete picture.
i am working on a simulation for wireless networks. so there are a bunch of nodes (each of which is an object). Each node has some co-ordinate values assigned to it randomly. Now, each node (object) needs to compute the distance from itself to all other nodes (objects). As i said earlier, i want each object to do it independently. so each object (say A) calls A.find_neighbors(); now, inside find_neighbors() - i want a pointer to other objects so that i can get that object's co-ordinates. thanks sabeel ps: each node (object) has an ID - which is just an integer value. |
Nice project :) Thanks for the clarification on the design.
I'm assuming that every node has a list of (pointers to) known nodes. What you could do is when A calls get_neighbours() it could call getList() for all the nodes in its list. getList() could return an array of pointers to nodes and change an int to inidcate how big the array is. Then you could add those pointers to A's known nodes list. You could use a linked list for the known nodes list to solve the duplicate known nodes issue. Sorry for the convoluted description.. maybe some pseudocode might be better... PHP Code:
There is also the issue of discovering a node when you start with an empty list but I assume you already have some way of dealing with that. I'm sure some OO guru will give you a better and more simple solution but thats probably how I would approach the problem :) |
hey einherjar,
Thanks for the suggestion. sabeel |
my C++ is a bit dodgy but how about this, the Node class keeps track of all nodes in a static vector, obviously get rid of position and distance and re-implement them your own way. also consider a list instead of a vector but that really depends on what your doing.
Code:
#include <iostream> |
it might be a bit of work to set up but if you want a 'good' solution you should try using a graph structure for the network and putting the distance calculating bit in there.
class Network { private: set<Node> nodes public: set<Node> getNeighbours(Node n) { set<Node> res; insert_iterator<set<Node> > nodeInsert(res); set_difference(nodes.begin(), nodes.end(), &n, &n +1, nodeInsert); } void addConnection(Node &n1, Node &n2) { nodes.insert(pair<Node, Node>(n1, n2)); nodes.insert(pair<Node, Node>(n2, n1)); } } with this you can centralise the functionallity but have it hidden from the main program. |
All times are GMT -5. The time now is 10:08 PM. |