std::ifstream in ("in.txt")
as the condition in a selection statement is not an expression, it is still an initialised declaration of a block-scope name. Selection statements choose one of several flows of control. selection-statement: if ( condition ) statement if ( condition ) statement else statement switch ( condition ) statement condition: expression attribute-specifier-seq opt decl-specifier-seq declarator = initializer-clause attribute-specifier-seq opt decl-specifier-seq declarator braced-init-list ... The value of a condition that is an initialized declaration in a statement other than a switch statement is the value of the declared variable contextually converted to bool (Clause 4). If that conversion is ill-formed, the program is ill-formed. The value of a condition that is an initialized declaration in a switch statement is the value of the declared variable if it has integral or enumeration type, or of that variable implicitly converted to integral or enumeration type otherwise. The value of a condition that is an expression is is the value of the expression, contextually converted to bool for statements other than switch; if that conversion is ill-formed, the program is ill-formed. .... |
We know from Section 1.5(p. 20)that ordinarily class definitions go into a header file. In this section we'll see how to define a header file for the Sales_item class. In fact, C++ programs use headers to contain more than class definitions. Recall that every name must be declared or defined before it is used. The programs we've written so far handle this requirement by putting all their code into a single file. As long as each entity precedes the code that uses it, this strategy works. However, few programs are so simple that they can be written in a single file. Programs made up of multiple files need a way to link the use of a name and its declaration. In C++ that is done through header files. To allow programs to be broken up into logical parts, C++ supports what is commonly known as separate compilation. Separate compilation lets us compose a program from several files. To support separate compilation, we'll put the definition of Sales_item in a header file. The member functions for Sales_item, which we'll define in Section 7.7 (p. 258), will go in a separate source file. Functions such as main that use Sales_item objects are in other source files. Each of the source files that use Sales_item must include our Sales_item.h header file. |
A header provides a centralized location for related declarations. Headers normally contain class definitions, extern variable declarations, and function declarations, about which we'll learn in Section 7.4 (p. 251). Files that use or define these entities include the appropriate header(s). Proper use of header files can provide two benefits: All files are guaranteed to use the same declaration for a given entity; and should a declaration require change, only the header needs to be updated. |
Headers Are for Declarations, Not Definitions When designing a header it is essential to remember the difference between definitions, which may only occur once, and declarations, which may occur multiple times (Section 2.3.5, p. 52). The following statements are definitions and therefore should not appear in a header: |
using namespace xyz;
is bad, especially in global scope. using specific::name;
is fine in a local scope.
using
keyword.using namespace std; // or any other namepace Is really bad.
using std::cout; // fine, or anything similar, can now use cout unqualified
std::cout
and similar examples, you will notice that experts and any partially decent coders always do that, rather than using std::cout;
. Have a look at any of JLBorges code.using bg = boost::geometry; // fine, now we can use bg elsewhere. This is an alias
typedef
.http://en.cppreference.com/w/cpp/language/type_alias |