C ++ programming thought notes

Source: Internet
Author: User

Chapter 3 C in C ++
1. hxx. HPP is also the C ++ header file
2. Write portable C ++Program: Use the C ++ standard library and use POSIX-compliant functions whenever possible
3. the header file <climits> and <cfloat> define the maximum and minimum values that may be stored for different basic data types.
4. sizeof is an operator that is not a function. If you use it for a variable, do not use parentheses: int I = sizeof I;
5. the pointer of the same type is subtracted, and the result is the number of elements separated by two pointers.
6. Convert variables and expressions into strings: Add a # Before a parameter in a Preprocessor macro. The Preprocessor converts this parameter to a character array.
# Define P (a) cout <# A <": result" <A <Endl;
7. ## flag pasting: it allows two identifiers to be set and pasted together to automatically generate a new identifier.
# Deifine field (a) char * A ##_ string;
Field (my) ;==> char * my_string;

Chapter 5 hiding implementation
1. youyuan
Class My {
Public:
Friend void g (); // youyuan: Global Function
Friend Class Z; // youyuan: Class
Friend Y: Fun (); // youyuan: member functions of other classes
}
2. youyuan can be nested.

 

Chapter 7 function overloading and default parameters
1. Union (Union) can carry constructor, destructor, member functions or even access control, but it cannot be used as a base class during inheritance.
2. A union without a type name or identifier is called an anonymous union.
Int main (){
Union {
Int I;
Float F;
};
I = 12; F = 1.0;
}

 

Chapter 8 Constants
1. When temporary variables are passed to a function by reference, the parameter of this function must be referenced by const.
Class X {};

X fun () (return X ();}

Void G1 (X &){}
Void G2 (const X &){}

Main (){
G1 (fun (); // this line will cause build error: const temporary created by fun ()
G2 (fun ());
}
2. the const Instance Object of the class can only call the const member function. Non-const Instance Object of the class. You can call const and non-const member functions, but the non-const member functions are called preferentially.

 

Chapter 10 namespace
1. Construction and destructor of static objects
The constructor of global variables and objects is called before Main (), and the storage space is allocated in the static storage area.
All static objects, whether global or local, are called after main ().
If a function that contains a local static object has never been called, the constructor of this object will not be executed.
2. Internal Connection
Within the File Scope, an object or function explicitly declared as static is internally connected.
One advantage of internal connection is that this name can be placed in a header file without worrying about conflicts during the connection.
The names that are usually included in the header file. Such as constants, inline functions, and internal connections by default (of course, constants are only internal connections in C ++ by default, and external connections in C)
3. namesapce can have aliases. A namespace can be defined by an identifier in a multi-head file to add content to the namespace.

 

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.