1. make their own habits C ++

tags: Effective C++

Accustoming Yourself to C++

Terms 01: as C ++ is a language federal

View C++ as a federation of languages.

  • Efficient C ++ Programming Code, as the situation varies, depending on which part of your use of C ++.

Terms 02: try to const, enum, inline replace #define

Preper consts,enums and inlines to #defines.

  • For simple constants, or preferably in const object enums replaced #defines.
  • For the shape of a macro function (macros), preferably use the inline function replacement #defines.

Both can be used to define and const define a constant, but when const definition defines the type of constants, the more accurate number. #define simply text replacement, in addition to define constants, but can also be used to define some simple functions, somewhat similar to inline (Inline).


Terms 03: Whenever possible, use const

Use const whenever possible.

  • Some things will be const help the compiler can detect errors usage. Const objects may be applied to any of scope, function arguments, return type of the function, the function of the body member.
  • The compiler enforces bitwise constness, but you should use the "constants of the conceptual" (conceptual constness) when writing programs.
  • When const and non-const member functions has achieved substantial equivalents, so that non-const version call the const version avoids code duplication.

const Usage:

  • const and pointers

    int a = 5;
    int * const p = & a; // pointer constant
     const int * p = & a; // constant pointer
    
  • const reference
    const reference point corresponding to the variable const const pointer, which points to the point, and the contents are allowed to change. So when the function returns a const reference, not by the function returns a reference to the actual object to make any changes.

  • const and function
    1, const Return Value modification: a function call that can not be used as an expression value left.
    2, const modified parameters: the parameter passed in the function can not be changed.
    3, const function: Any attempt to change the member variables and call non-const member functions of operation are considered illegal.

  • const and class/
    1, const initialization data member can only be performed in the initialization table constructor of the class;
    2, const const object can only invoke a function as an ordinary function may modify a member variable. (Ordinary objects can either call the const function, you can also call ordinary function.)


Terms 04: determine before the object is first initialized using

Make sure that objects are initialized before they’re used.

  • Manual initialization is built-in objects, because C ++ does not guarantee initialize them.
  • Constructor is preferable to use the initial value column member (member initialization list), rather than using an assignment operator (Assignment) constructor body. The initial value of the member variable column lists, order over and they should be declared in the same order in class.
  • For the removal of "cross-compilation unit of the initialization order" problem, replace the non-local static objects with local static objects.

Reference article:
《Effective C++》
C ++ usage at different locations in the const
C ++ inline functions (C ++ inline) Explanation

Intelligent Recommendation

Record your own IDEA habits

Quick arrival Every time you change your computer, the IDE is re-tossing, this record, I started ~ basis Font 2. Theme Every time you change your computer, the IDE is re-tossing, this record, I starte...

Let yourself habits C ++ (first part) (Terms 1 ~ Terms 4)

Terms 1: Visual C ++ is a language federal Four language parts of C ++:C, Object-oriented c++, template c++, STL please remember: C ++ efficient programming Code varies depending on the situation, dep...

Make your own website (1) - CentOS 7

First, build a LAMP environment  ...

Use BootStrap to make your own menu (1)

First look at the picture This is the big screen When this is a small screen This is when the small screen menu is expanded   among them 1.Lang = “en” in The lang attribute written in...

Make your own image data (1)

Use image data to create a data format that can be input to Tensorflow Folder format: ROOT_FOLDER | ### Ideas: When we learn Tensorflow, we generally import mnist data directly. For the types of image...

More Recommendation

C # programming -139: Make your own browser

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using&nbs...

Make your own C function library

Header file my_func.h Source file my_func.cpp...

C # make your own packaging tool

C # make your own packaging tool, the technology used is actually used in the C # inside"Dynamic Compilation"technology. The following simply describes the flow: 1. Your packaging tool has a...

1 code style and habits

Java:  ...

Python good habits (1)

Control flow If related Do not put variables andTrue False None () [] {}Direct comparison if foo == True: should be written as if foo: Check variables in the list But If not should be written in one l...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top