Computer Science Canada generic code for writing structures, help me make it more generic |
Author: | chopficaro [ Tue Sep 16, 2008 12:01 pm ] | ||||||
Post subject: | generic code for writing structures, help me make it more generic | ||||||
well ive got a program that scans a structure from cin and puts it in a binary file, and can also take it out of the binary file and display it. i like the interface ive made very much, i want to use this code for many many projects without having to change much, especially for many different types of structures, so im trying to make it more generic, but i havent a clue how to proceed. my guess is that ill have to use some element of classes. im not asking u to write my code i just want a hint. anything at all would be greatly appreciated take a look: exportimportstructmain.cpp
exportimportstructdef.cpp
exportimportstructdec.h
|
Author: | Rigby5 [ Thu Oct 09, 2008 10:43 pm ] |
Post subject: | RE:generic code for writing structures, help me make it more generic |
Not too bad of a start. The difference between classes and structs is really insignificant, in how the default public vs private. So you don't really need to use classes. You can add member functions to a struct as well. But not many people do that, so I would switch to classes if you need member functions to manipulate the data in a group like a struct or class. So how do you make it more generic? Right now you have a fixed number of 4 integers in the struct. A more generic way would be to have an integer that tells you how many values is stored, and then have an array of that size that actually has the integers in it. That way the number can grow or shrink as necessary. Initially the counter value would be 0, and the array would be of size 0, or just a pointer of type integer. But every time you wanted to change the amount of data, you still would need make a new array, copy over any old data to the new array of a different size, and delete the old array. A better way is to not use arrays at all, but to make a linked list. Each node is a struct with only a single data value, and a pointer to the next struct in the list. If you don't want to learn about linked lists, then you could use an existing one, like from the Standard Template Library, (STL), which has things like VECTOR, etc. |
Author: | OneOffDriveByPoster [ Fri Oct 10, 2008 8:55 pm ] | ||
Post subject: | Re: generic code for writing structures, help me make it more generic | ||
Maybe what you are looking for are templates, something like:
|