Need advice on function structuring...

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2007
Posts: 35
Reputation: Ratte is an unknown quantity at this point 
Solved Threads: 0
Ratte's Avatar
Ratte Ratte is offline Offline
Light Poster

Need advice on function structuring...

 
0
  #1
Nov 13th, 2007
Before I write this, I realize there are way better ways to accomplish what I am asking, but I am JUST wondering if it is possible to do it the way I am asking.

I have a main function called system. The system has a menu and depending on input, it
calls subfunctions to do user requisted tasks.

The user will supply a file that contains some data. The first line of the data is the size of the structure in which this data is to be stored (A graph consisting of an array of singly linked lists). I would like one function to read, create and populate this array from the file, then use another function to analyze it.

The problem I am encountering is how can the other function access this structure if it is to be dynamicaly created within the first function? (therefore a temporary object)

This works fine if I combine everything in one function, I am just wondering if it is somehow possible to make this structure reusable once one function has built it (remember, it is not part of the system because the size is not known until the user supplies the file). I am guessing one solution is for the first function to return this structure? Is there an easier way? This solution would require a complicated copy constructor.

Thanks.

PS. I would like to thank Vijay for helping me out with this (school) project so far.
Last edited by Ratte; Nov 13th, 2007 at 1:41 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,952
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Need advice on function structuring...

 
0
  #2
Nov 13th, 2007
Actually, you are doing things the Right Way. Five stars for you for using your head.

Things are only temporary if you don't plan to use them long. In C++, you can pass things around easily enough and extend their life beyond normal even.

Local variables only live as long as the function they are declared in. But stuff you allocate on the heap is global. You only use local variables to play with it.

Here's a fun program you can play with:
  1. #include <iostream>
  2. #include <sstream>
  3. #include <cctype>
  4. using namespace std;
  5.  
  6. struct node {
  7. string name;
  8. int age;
  9. node* next;
  10. };
  11.  
  12. node *get_list() {
  13. node *first = NULL;
  14. node *current;
  15. string name, s;
  16. do {
  17. cout << "Enter a name: ";
  18. getline( cin, name );
  19. cout << "Enter " << name << "'s age: ";
  20. getline( cin, s );
  21.  
  22. if (first == NULL) first = current = new node;
  23. else {
  24. current = current->next = new node; // pay attention here!
  25. current->next = NULL;
  26. }
  27. current->name = name;
  28. if (!(stringstream( s ) >> current->age)) current->age = 0;
  29.  
  30. cout << "More (y/n)? ";
  31. getline( cin, s );
  32. } while (toupper( s[ 0 ] ) != 'N');
  33.  
  34. return first;
  35. }
  36.  
  37. void print_oldest( node *list ) {
  38. node *oldest = list;
  39. for (; list != NULL; list = list->next)
  40. if (list->age > oldest->age) oldest = list;
  41. if (oldest != NULL)
  42. cout << oldest->name << " is the oldest.\n";
  43. }
  44.  
  45. node *delete_list( node *list ) {
  46. node *n;
  47. while (list != NULL) {
  48. n = list;
  49. list = list->next;
  50. delete n;
  51. }
  52. return NULL;
  53. }
  54.  
  55. int main() {
  56. node *list = get_list();
  57. print_oldest( list );
  58. list = delete_list( list );
  59. }
Last edited by Duoas; Nov 13th, 2007 at 2:02 am. Reason: Fixed a bug. Sorry.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,836
Reputation: ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all 
Solved Threads: 119
ithelp's Avatar
ithelp ithelp is online now Online
Posting Virtuoso

Re: Need advice on function structuring...

 
0
  #3
Nov 13th, 2007
You can use type caste to convert it.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,952
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Need advice on function structuring...

 
1
  #4
Nov 13th, 2007
/me re-reads the first post

Ah, just use new to create an array big enough to hold the head of each linked list.
node **listarray = new node*[ number_of_lists ];
Then return listarray.

Hope this helps.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 35
Reputation: Ratte is an unknown quantity at this point 
Solved Threads: 0
Ratte's Avatar
Ratte Ratte is offline Offline
Light Poster

Re: Need advice on function structuring...

 
0
  #5
Nov 13th, 2007
Thanks Duoas, I didn't realize this was possible. I thought the same applied as with local variables.

So it is not possible to return a pointer to the graph then? I would return an array of pointers with elements of the graph?

Here is my graph:

  1. template <typename T>
  2. class Graph {
  3.  
  4. public:
  5. explicit Graph ( int size ) : SIZE (size), datar( new SinglyLinkedList<T> [size] ) {}
  6. ~Graph() { delete [] datar; }
  7. Node<T> *find(T);
  8. bool AddVertex(T);
  9. bool AddEdge(T, T);
  10. Node<T>* getList (int);
  11.  
  12. private:
  13. const int SIZE;
  14. SinglyLinkedList<T> * datar;
  15.  
  16. };
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,952
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Need advice on function structuring...

 
0
  #6
Nov 13th, 2007
So why can't you do:
  1. template <typename T>
  2. Graph<T> *get_data() {
  3. Graph<T> *result = new Graph<T>;
  4. // fill graph from file here
  5. return result;
  6. }
It is the same as returning a pointer to anything else you dynamically allocate.
Last edited by Duoas; Nov 13th, 2007 at 5:41 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 35
Reputation: Ratte is an unknown quantity at this point 
Solved Threads: 0
Ratte's Avatar
Ratte Ratte is offline Offline
Light Poster

Re: Need advice on function structuring...

 
0
  #7
Nov 13th, 2007
Thank you Duoas, you have been very helpful.

I chose not to do it that way initially because I wanted to keep my Graph function nice and clean, but I do see the advantage in doing it that way. I am now more confident in passing complex (to me, anyway) structures around!
Last edited by Ratte; Nov 13th, 2007 at 12:52 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,952
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Need advice on function structuring...

 
0
  #8
Nov 13th, 2007
Glad to be of help. Just don't forget to delete things when you are done with them.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC