CFileHandler's ifs member

CFileHandler's ifs member

Discuss the source code and development of Spring Engine in general from a technical point of view. Patches go here too.

Moderator: Moderators

Post Reply
Muon
Posts: 24
Joined: 23 Jan 2012, 21:42

CFileHandler's ifs member

Post by Muon »

Why does CFileHandler use an std::ifstream* instead of just an std::ifstream? https://github.com/spring/spring/blob/7 ... dler.h#L73
Kloot
Spring Developer
Posts: 1867
Joined: 08 Oct 2006, 16:58

Re: CFileHandler's ifs member

Post by Kloot »

Go back in time about eight years (FileHandler is that old) so you can ask the original author.

Any reason this particular file holds your interest? ;)
Muon
Posts: 24
Joined: 23 Jan 2012, 21:42

Re: CFileHandler's ifs member

Post by Muon »

Yes, I saw, I checked the history. It holds my interest because it's peculiar, has no comment, and I am interested in starting with Spring development, mostly to help clean up code :-) I'm actually rather interested in working on http://springrts.com/wiki/GSoC_New_rendering_system , but perhaps I should start small to learn Spring.
zerver
Spring Developer
Posts: 1358
Joined: 16 Dec 2006, 20:59

Re: CFileHandler's ifs member

Post by zerver »

The probable reason is the the original author thought that you need to know about the associated file to construct the object.
User avatar
jK
Spring Developer
Posts: 2299
Joined: 28 Jun 2007, 07:30

Re: CFileHandler's ifs member

Post by jK »

No, the original author was smart and didn't included <ios> or <fstream> in the header and so speedup the compilation time.
Muon
Posts: 24
Joined: 23 Jan 2012, 21:42

Re: CFileHandler's ifs member

Post by Muon »

Actually, he did include <ios>. And anyway, <ios> isn't enough; it was just good fortune that it worked. std::ifstream is defined in <fstream>, and you're not allowed to forward declare stuff in std at all.
User avatar
jK
Spring Developer
Posts: 2299
Joined: 28 Jun 2007, 07:30

Re: CFileHandler's ifs member

Post by jK »

Muon wrote:Actually, he did include <ios>. And anyway, <ios> isn't enough; it was just good fortune that it worked. std::ifstream is defined in <fstream>, and you're not allowed to forward declare stuff in std at all.
You don't need to include anything when you use pointers in headers. The includes are then only needed where the implementation happens (.cpp). The <ios> was added for an additional function param of Seek().
Muon
Posts: 24
Joined: 23 Jan 2012, 21:42

Re: CFileHandler's ifs member

Post by Muon »

jK wrote:
Muon wrote:Actually, he did include <ios>. And anyway, <ios> isn't enough; it was just good fortune that it worked. std::ifstream is defined in <fstream>, and you're not allowed to forward declare stuff in std at all.
You don't need to include anything when you use pointers in headers. The includes are then only needed where the implementation happens (.cpp). The <ios> was added for an additional function param of Seek().
No, in most cases, you only need to forward declare the types. But as I said, you are not allowed to forward declare things in std, hence including <fstream> is necessary either way.
Kloot
Spring Developer
Posts: 1867
Joined: 08 Oct 2006, 16:58

Re: CFileHandler's ifs member

Post by Kloot »

Forward declarations (or include directives) are only needed if you want a header to be compilable by itself:

Code: Select all

// bla.hpp (*no* includes, *no* fwddecs)
void f(std::fstream*);

// bla.cpp
#include <fstream>
#include "bla.hpp"
Regardless of whether this would be bad practice, STL headers do tend to drag in a lot of crap, and gcc already isn't all that fast.
Muon
Posts: 24
Joined: 23 Jan 2012, 21:42

Re: CFileHandler's ifs member

Post by Muon »

Well, sort of. You can't actually include that header and still have the .cpp compile without a declaration of std::ifstream somewhere, making that kinda useless. As a rule, include only what you need, but include everything you need. If I actually wanted just a pointer/reference, I would just include <iosfwd>, as it provides forward declarations for all iostreams.
Post Reply

Return to “Engine”