943,733 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 906
  • C++ RSS
Nov 13th, 2007
0

Need advice on function structuring...

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
Ratte is offline Offline
38 posts
since Oct 2007
Nov 13th, 2007
0

Re: Need advice on function structuring...

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:
C++ Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Nov 13th, 2007
0

Re: Need advice on function structuring...

You can use type caste to convert it.
Reputation Points: 769
Solved Threads: 128
Banned
ithelp is offline Offline
1,910 posts
since May 2006
Nov 13th, 2007
1

Re: Need advice on function structuring...

/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.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Nov 13th, 2007
0

Re: Need advice on function structuring...

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:

C++ Syntax (Toggle Plain Text)
  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. };
Reputation Points: 10
Solved Threads: 0
Light Poster
Ratte is offline Offline
38 posts
since Oct 2007
Nov 13th, 2007
0

Re: Need advice on function structuring...

So why can't you do:
C++ Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Nov 13th, 2007
0

Re: Need advice on function structuring...

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
Ratte is offline Offline
38 posts
since Oct 2007
Nov 13th, 2007
0

Re: Need advice on function structuring...

Glad to be of help. Just don't forget to delete things when you are done with them.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: I've heard about a compiler/debugger. Which is it?
Next Thread in C++ Forum Timeline: How many loops





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC