View Full Version : Coding Structure
09-27-2004, 05:44 PM
More of a general coding question this one, but could anyone point me in the direction of a page or site, or even recommend a good book dealing with code structure.Eg where to put the likes of fstreams in a big project, like in an FStream.h file or in the .cpp where you are using them ??
This topic has always confused me a little.
09-27-2004, 05:49 PM
Excellent information, especially if you have at least a general idea of what you want. :)
09-27-2004, 06:08 PM
Actually, I think I misread your post lol - if you meant where do you put declarations and the like, the general rules I've been taught to follow are that three basic forms are used: header files, main, and implimentation files. Headers are naturally where all of your declarations go, prototypes and objects included. It typically isn't recommended to make fstream variables global... just pass them as paramaters - fstream& As a matter of fact, most teachers seem to discourage globals period since their ability to be accessed from anywhere make them a bit treacherous. The main.cpp contains only the main function, and implimentation.cpp is typically just that. Other than that, it doesn't matter how you divy it up, provided its logical to follow. I hope this is some help
09-27-2004, 06:22 PM
Thanks jimmy.Sorry I didnt phrase my question the best.But yea your second answer was more like what im getting at.The cprogramming site did have some very interesting stuff in the faq section though :)
Yea ive heard it somewhere before not to declare fstreams as globals cause of mixups when closing files etc. Would you be able to give me an example of what exactly your talking about in your second post by any chance ?
09-27-2004, 06:36 PM
Once you've declared the fstream, say its an input file, and opened it:
infile.open("file.dat");you can do your normal manipulations in the function in which you've opened it. In other words, if you want to open it in another function then declare and open it there - it will then be local to that function alone. In that way, you can pass information in and out through the function paramaters. Say you want ints:
void get_int(int&); //can also be (int& nameOfInt if you want. The ampersand makes sure the value gets changed for the return. Or you can return it as the function value
int get_int(); //return infile>>int_name; If you open the file in the main.cpp, pass it to the function that will use it. Here's the prototype:
void takeMyFile (ifstream&) //Add the file name like (ifstream& filename) if you want to lock it for the function, use
void get_int(ifstream& filename)
}and so on. If you want to use the fstream as a class private member, then you can only access it in class functions, but that's more complex lol... a little. Hope this helps
09-27-2004, 06:53 PM
Thanks again jimmy
Just wondering how heavy declaring and killing file streams as local variables is on cpu time.Do ya have any idea ?
09-27-2004, 07:05 PM
That one I wouldn't know - I'd imagine its much more intensive than passing the file, if you were thinking of closing then reopening. The parameter is just a pointer reference to the file, so its only the size of the address and the interfunction communication has got to be small. Then again, I just guessing lol
Powered by vBulletin® Version 4.2.3 Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.