C ++ memo

Source: Internet
Author: User
Document directory
  • The static inline function in the header file. If a function is defined in the header file, if more than two files include

Extern:

Declare a variable. Trap: if an initialized variable is declared, extern is ignored. (That is, it is a definition)

extern int var = 30 ;     // defintion varextern nt s ;             // declares s

Const:

Const variable has an address. Do not want some languages to be symbolic constants.

The default scope of const variable is the current compilation unit (file). Other files cannot be accessed and must be globally specified using extern. Note: Non-const variables are global by default.

const int ci = 32 ; //access scope is fileextern const int eci = 12; // globle scope


Reference:

Definition: Add & to define the variable name.

int i = 3 ;int &ri = i ;

Default real parameters:

If a parameter has a default real parameter, the parameter following it must define the default real parameter.

Define constraints: You can specify them in the prototype or when defining a function. Only one default real parameter can be specified in a file. Generally, it cannot be specified at the same time. (Generally, the function declaration is specified in the header file)

Ptrdiff_t:

It is a machine-independent signed integer type. Arithmetic return type used as a pointer

Inline Function:

Add inline keyword before the return type when defining the function. (This is not required when declaring the function. You can also add this parameter)

The same nature of the inline function and the const variable is that the scope is the compilation unit. To enable other compilation units, you must add extern (the same as the const variable) when defining the function)

extern inline aad() {...} 

The inline function should be placed in the header file.

  • Explicitly specify the inline member function: Same as inline, you can specify the inline in the declaration, and you can specify the inline in the definition. Inline member functions are generally placed in the header file of the class definition.

Overload:

  • Function: It can be overloaded (that is, different) only when const and non-const references or pointers are used as parameters. Other types of const and non-const parameters are the same.
  • Element function: it can also be reloaded based on whether it is const * This. That is, myclass & ele_func (...); const myclass & ele_func (...) const;

I/O:

Cin> STD: Ws; ignore the following blank characters to know the next valid character, only valid for the next input.

Int X, Y; string STR; CIN> x> Y; cin. ws; CIN> STR;
For example, I enter 3 24 Hello (Press ENTER) if there is no cin. if ws is used, the STR value is "Space hello". If yes, it is "hello". If I enter 23 12 (Press ENTER) Hello, if there is no cin. WS indicates that the STR value is null. If STR is hello

Class:

  • List of constructor initialization, specified in the Function Definition
    Myclass() : ele1(val1),  ele2(val2) {..}
  • Prohibit other functions from calling the copy constructor of the class. A private constructor can only be declared and not defined if you want to make friends unable to access the constructor. (An error will be reported during the link)

Member function pointer, pointer of the virtual member function # include <iostream>
Struct {
Virtual void OK (int I) {STD: cout <"A" <I <STD: Endl ;}
};

Struct B: Public {
B () {OK (0 );}
Virtual void OK (int I) {STD: cout <"B" <I <STD: Endl ;}
};

Int main (){

Void (A: * pA) (int I) = & A: OK;
Void (B: * pb) (int I) = & B: OK;
B;
(B. * pA) (0x33); // b0x33
(B. * pb) (0x44); // b0x44
Return 0;
}
1. When defining the member function pointer, both the variable and the member function must use the class name.
2. class objects must be used to call member function pointers.
2. The virtual member function pointer and the virtual function will find the matching call function.

Firnd statement

When declaring that Class A is a member of Class B, you must use the class keyword

E. g

Class {};

Class B {frind Class ;}

STL

<T>: size_type: a machine-independent unsigned integer. Is the replacement type of T. Size (); The subscript type of string.

<T>: difference_type: a machine-independent signed integer type. It is mainly used for the return type of arithmetic operations of the iterator.

Static

It is mainly used to control the scope of declared objects.

Variables and functions declared using static are only visible in the current compilation unit (file)

If you use static to define a variable or function in the header file, other files that include this file have their own local variables.

The static inline function in the header file. If a function is defined in the header file, if more than two files include

This definition file will have multiple definition errors when it is linked. You can use static to declare the function so that the function is only visible in this file. No error will be reported.

Generally, small commonly used functions are declared as static inline.

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.