Valid tive C ++ version 2 1) const and inline 2) iostream

Source: Internet
Author: User
Tags constant definition

Clause 1 try to use const and inline instead of # define

> "Try to use the compiler instead of preprocessing"
Ex. # define ASPECT_R 1.653 the compiler will never see the ASPECT_R symbol name. It will be removed by the Preprocessing Program before the Source Code enters the compiler, and ASPECT_R will not be added to the symbol list. When an error is reported during compilation, the error message points to 1.653, which prevents programmers from tracking errors. (This problem also occurs in the symbol debugger );

> Solution: defines the constant const double ASPECT_R = 1.653; the constant definition is generally in the header file, and many source files will contain it; [me: if it is only a locally used constant, it should be placed in cpp]
Note: When defining pointer constants, in addition to defining the type referred to by the pointer As const, the pointer is often defined as const; Ex. const char * const kName = "Scott ";

Define the constants of a class and limit the constants to the class. To ensure that a constant has only one copy, define it as a static member:

123456 class GamePlayer {private: static const int NUM_TURNS = 5; // constant declaration (initialized when the old compiler does not accept static member declarations) int scores [NUM_TURNS]; // use of constant ...};

Note if the preceding statement is just a NU_TURNS Statement (without '= 5'), you must define the static member of the class in the class implementation code: const int GamePlayer: NUM_TURNS = 5; // mandatory definition; goes in class impl. file;

[This may be a write inverse. If the header file contains '= 5', it is already defined and does not need to be defined in cpp.]


Only static members of the integer type can be initialized in the Note class: int, bool, char...

The Issue must define static members in the header file: when your class needs to use constants of this class: e.g. gamePlayer: scores array declaration (the array SIZE needs to be known during compilation); [SIZE must be a static constant or enumeration number]

Solution for compilers that do not support defining static constants in header files, borrow enum and use the enumeration type when int type is required:


1234567 class GamePlayer {private: enum {NUM_TURNS = 5}; // "the enum hack"-makes // NUM_TURNS a symbolic name for 5 int scores [NUM_TURNS]; // fine ...}

# The define command implements a macro that looks like a function, but does not call the function;


# Define max (a, B) (a)> (B )? (A): (B) Ensure that each parameter is enclosed in parentheses, even if this is still unexpected:

123 int a = 5, B = 0; max (++ a, B); // The value of a is increased by 2 times max (++ a, B + 10 ); // The value of a is increased only once.

> Max running is determined based on the value;


Use inline functions instead: inline int max (int a, int B) {return a> B? A: B ;}

Use a template to extend types other than int:

123 template <class T> inline const T & max (const T & a, const T & B) {return a> B? A: B ;}

> The template generates a set of functions. Each function compares two objects of the same type and returns a large value;


Note: Before using a template to write general functions such as max, check whether the standard library already exists;


Const and inline reduce the use of preprocessing, but # include is not missing yet. # ifdef/# ifndef is also important in controlling compilation;

 


Clause 2 <iostream> instead of <stdio. h>

Scanf/printf is lightweight, efficient, but not type-safe, with no scalability. You need to separate the read/write variables from the information in the control read/write format (FORTRAN style );

>>And <, implementing overload functions can process different types. The read/write syntax is the same, and you do not need to remember the format rules;

12345678910111213141516 int I; Rational r; // Rational quantity cin> I> r; cout <I <r; class Rational {public: Rational (int numerator = 0, int denominator = 1 );... private: int n, d; // molecule, denominator: friend ostream & operator <(ostream & s, const Rational & r );}; ostream & operator <(ostream & s, const Rational & r) {s <r. n <'/' <r. d; return s ;}

> "Opertaor <" is not a member function (operator );


Relatively insufficient:

1) Some iostream operations are less efficient than the corresponding C stream operations;

2) during the standardization process, the iostream library has made many changes at the underlying layer. For programs requiring maximum portability, different vendors have different standards;


3) The iostream Library Class has constructor, <stdio. h> the function in does not exist. When some static object initialization sequence is involved, it is simpler and more practical to use the standard C library;

Advantages: iostream library classes and function types are secure and scalable;

Library inclusion: <iostream>

# Include <iostream. h> to obtain the iostream library elements in the global space;

# Include <iostream> obtains the iostream library elements placed under the namespace std;

Elements retrieved from the global space may cause name conflicts;

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.