C ++ primer Reading Notes (2) -- Basic Knowledge

Source: Internet
Author: User

1. const

Const int a = 23;

If const int a = 23 is defined in file1.c, The file2 file in the same program cannot use the extern to use a in file1,

// file1extern const int a =12 ;// file2extern const int a ; // use a from file1

The default value of a non-const variable is extern,

2. reference: Another name of the object.

.

The reference must be initialized with an object of the same type as the reference.

int ival = 1024 ;int &refval0 = ival ; // okint &refval1 ;  // error :must be intialzedint &refval2 = 10 ;  // error :initilizer must be an object

After the reference is initialized, as long as the reference exists, it is bound to the object specified during initialization. It is impossible to bind a reference to another object.

.

Const reference: a reference to a const object.

const int ival = 1024 ;const int &refval = ival ;//ok: both reference and object are constint &refval = ival ; // error: nonconst reference to a const object 

Refval can be read but cannot be modified

That is, the const reference is a reference pointing to the const.

, Such as the literal value constant:

int i = 42 ;const int &r = 42 ;const int &t = r + i ;double dval = 3.14 ;const int &r=dval ;

The compiler converts the code into the following encoding:

int temp = dval ;const int &r = temp ;

3.

 class A{    public:       ...     private:       ...} ;

New programmers often forget the class definition. This is a common mistake!

There is a very important difference between defining class data members and defining variables: Class Members cannot be initialized as part of their definition ..

4.

 struct A{// operation    private:// data};

The only difference between the struct and the class keyword definition class is the default access level: by default, the struct member is public, and the class member is private.

5. the header file is contained in multiple source files, so it should not contain the definition of variables or functions. However, the header file can be included. The sum is known at compilation. These entities can be defined in multiple source files, as long as the definitions in each source file are the same.

6. pre-processor:

# The include facility is part of the c ++ preprocessor.

System header files may be saved in a more efficient format specific to the compiler.

The header file can contain other header files. Therefore, when designing the header file, it should be included multiple times in the same source file. We must ensure that multiple times containing the same header file will not cause the class and object defined by this header file to be defined multiple times.

The general approach to ensure header file security is defined using a pre-processor.

The Preprocessor constant name must be unique in the program. Any use of names that match the Preprocessor constant is associated with the Preprocessor constant. To avoid name conflicts, Preprocessor constants are often expressed in uppercase letters.

You can avoid multiple definitions of the same head file as follows:

#ifndef CAR_H#define CAR_H  // definition of  class car and related function goes here #endif

Header files should contain protection characters, even if these header files are not included by other header files. Writing header file protection characters is not difficult. If the header file is contained multiple times, it can avoid compilation errors that are hard to understand.

We can use entities (such as classes) defined in header files to name Preprocessor constants to avoid the problem of duplicate names of Preprocessor constants.

# Include <iostream> standard header file. The compiler searches for this header file in the predefined location set. You can modify these predefined locations. The search methods used vary with compilers.

# Include "our_header.h" is a non-system header file. Searching for non-system header files usually begins with the path of the source file.

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.