I disagree with Andy about the header:
Functions.h
1 2 3 4 5 6 7 8 9 10
|
#ifndef UNTITLED_FUNCTIONS_H
#define UNTITLED_FUNCTIONS_H
#include <string>
#include <iosfwd> // forward declares std::istream
void readResp( std::istream& infile,
std::string* resp, std::string* categories,
size_t size );
#endif //UNTITLED_FUNCTIONS_H
|
First and foremost, Andy says that a header should not include any other headers. That means that anyone willing to use that header must know what other headers to include
before that header, or face compile errors.
Guess what? We can include <string> without first including quite many other Standard Library headers. Does that mean that the Standard Library headers have special exception from "Andy's Rule"?
No, it is perfectly ok and expected that a header includes
necessary
bits. What is necessary in this header? The std::istream and std::string do need at least a forward declaration.
The <iosfwd> provides forward declarations for most iostream types. That is sufficient.
There is no similar forward declaration header for std::string and thus the actual <string> is necessary.
Why did I write std::istream and std::string? Why the std::-prefix? I'm explicitly referring to the istream and string that are in the std-namespace. I don't rely on any using ...
Explicit is unambiguous.
Did I make a typo by writing istream and not ifstream? No. I assume that the function does not actually use any file-specific features of the stream. If the function requires ifstream, then it can be used only with ifstreams, but if it can take istream, then you can call it with any istream, including ifstream. That is more generic and reusable.
string* foo
vs
string foo[]
. For the compiler these are identical. In the function the foo is a pointer. You might want to keep the [], for you want to hint to the user that you expect a pointer to array.
int size
vs
size_t size
. The size_t is an unsigned integer type. I presume that the size knows the size of array(s). Can you have an array with -7 elements? No. The use of unsigned type hints that negative values are not logical.