| | |
Need advice on function structuring...
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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.
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.
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:
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)
#include <iostream> #include <sstream> #include <cctype> using namespace std; struct node { string name; int age; node* next; }; node *get_list() { node *first = NULL; node *current; string name, s; do { cout << "Enter a name: "; getline( cin, name ); cout << "Enter " << name << "'s age: "; getline( cin, s ); if (first == NULL) first = current = new node; else { current = current->next = new node; // pay attention here! current->next = NULL; } current->name = name; if (!(stringstream( s ) >> current->age)) current->age = 0; cout << "More (y/n)? "; getline( cin, s ); } while (toupper( s[ 0 ] ) != 'N'); return first; } void print_oldest( node *list ) { node *oldest = list; for (; list != NULL; list = list->next) if (list->age > oldest->age) oldest = list; if (oldest != NULL) cout << oldest->name << " is the oldest.\n"; } node *delete_list( node *list ) { node *n; while (list != NULL) { n = list; list = list->next; delete n; } return NULL; } int main() { node *list = get_list(); print_oldest( list ); list = delete_list( list ); }
Last edited by Duoas; Nov 13th, 2007 at 2:02 am. Reason: Fixed a bug. Sorry.
You can use type caste to convert it.
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:
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)
template <typename T> class Graph { public: explicit Graph ( int size ) : SIZE (size), datar( new SinglyLinkedList<T> [size] ) {} ~Graph() { delete [] datar; } Node<T> *find(T); bool AddVertex(T); bool AddEdge(T, T); Node<T>* getList (int); private: const int SIZE; SinglyLinkedList<T> * datar; };
So why can't you do:
It is the same as returning a pointer to anything else you dynamically allocate.
C++ Syntax (Toggle Plain Text)
template <typename T> Graph<T> *get_data() { Graph<T> *result = new Graph<T>; // fill graph from file here return result; }
Last edited by Duoas; Nov 13th, 2007 at 5:41 am.
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!
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.
![]() |
Similar Threads
- C++ advice (scanf/printf) (C++)
- Pointer logic advice (C)
- Windows programming - C - Save file function (C++)
- Passing char * to function and populating inside (C++)
- Please Give Us Advice On Redesigning the Corporate Website. (Website Reviews)
- Centering <div> (JavaScript / DHTML / AJAX)
- Keyboard function key "locked" but only in Windows (USB Devices and other Peripherals)
- Function[Array] in combination with cin>> (C++)
- How would i recode as a function? (Java)
Other Threads in the C++ Forum
- Previous Thread: I've heard about a compiler/debugger. Which is it?
- Next Thread: How many loops
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






