C + + Learning series One

Source: Internet
Author: User

Part I: Referencing vs pointers

meaning of the reference : the alias of the variable

Note : A variable cannot have only aliases, and must have a real variable corresponding to it

References to base data types

The operation of the alias itself is the same as the operation of its entity

1. References to basic data types

Type & variable Reference name = variable name

The above program output results are: 10

2. References to struct data types

Type & struct Reference name = struct-Body name

The above program output results are: 10 20

3. Reference to pointer type

Type *& pointer reference name = pointer

The above program output results are: 20

4. References as function arguments

The above program output results are: x=20,y=10

Note : It is recommended to use the program template on the right

FAQ: What are the differences and linkages that are referred to as pointers?

For:

★ Contact:
1. Is the concept of the address;
The pointer points to a piece of memory whose contents are the address of the referred memory, and the reference is the alias of a block of memory.

★ Difference:
1. The pointer is an entity, and the reference is only an individual name;
2. The reference does not need to dereference (*), the pointer needs to be dereferenced;
3. References can only be initialized once at the time of definition, and then immutable; pointers are variable;
4. The reference does not have a const, the pointer has a const;
5. The reference cannot be null, the pointer can be empty;
6. "sizeof Reference" gets the size of the variable (object) pointed to, and the "sizeof pointer" gets the size of the pointer itself (the address of the variable or object to which it is pointing);
7. The pointer and the reference self-increment (+ +) operation has different meanings;
8. From memory allocation: The program allocates memory regions for pointer variables, whereas references do not need to allocate memory areas.

More information: http://blog.csdn.net/lyd_253261362/article/details/4323691

———————————————————————————————————————————————————————————————————————————————————————————————————————————————

Part Two: The const that makes people love and hate

The basic function of const is to define a constant, meaning that it cannot be modified once defined.

1. Comparison of universal variables with const constants:

2. The writing and comparison of several common const

A well-remembered way to differentiate the int const *P from the int* const p, to read the * as a pointer to then read from the back forward:

The first int const *P can be read as p is a pointer to const INT,P are pointers to constants

The second int* const p can be read as a const pointer to int,p of p is a constant pointer to the INT type

Analysis:

Description

The value of the position indicated by pointer y in the first statement may change to affect X, so this operation is forbidden by the compiler

The second statement takes the value of the address of the variable x as the initial of the variable y, which is reasonable and therefore allowed

Exercises:

Correct option: A

Explanation: The value of the position that the pointer p refers to may change to affect a, so this operation is forbidden by the compiler

The difference between define and const definition constants

Define does not check for syntax errors, and const checks for syntax errors at compile time. It is therefore more recommended to use the Const method to define a constant.

—————————————————————————————————————————————————————————————————————————————————————————————————

The third part function new characteristic

1, the function parameter can set the default value, the default parameter list is given when the declaration, the definition of the time without giving

Note: Parameters with default parameter values must be at the far right of the parameter list

Program execution Description: no argument with default value, otherwise override default

2. Function overloading

1) meaning

Within the same scope,

Define multiple functions with the same function name ,

  The number of parameters differs from the parameter type

2) Using the example

3, inline function inline

1) comparison between an inline function and a normal function

2) Using the example

3) Instructions for use

    • Inline compilation is recommended for compilers, and implementation is determined by the specific compiler
    • Logic is simple (no loop branching structure), calling frequently used functions suggests using inline
    • Recursive functions cannot use inline functions

Summary

1, function parameter default value argument overrides default value

2, the function overload name the same parameter can be discerned

3. Inline functions with high efficiency and conditions


———————————————————————————————————————————————————————————————————————————————————————————————————————————————

Part IV Memory Management

1. Open question: What is memory management?

Thinking: What is the nature of memory? A resource

thinking: Who is in charge of memory resources? Operating System

thinking: What can we do? request/Return memory resources

2. Application and release of memory

Request Memory: New

Free Memory: Delete

3. How to apply and release memory

Application memory: int *p=new int; int a =new int (5);

Free memory: delete p;

4. Application and release of block memory

Request Memory: Int *arr=new int[1000];//request block memory

Free Memory: Delete []arr;//free block memory

5, memory of other management methods

C language

void *malloc (size_t size);

void *free (void *memblock)

C++

New

Delete

use suggestion : Use the matching, do not mix

6, the application of memory considerations

1) Memory application needs to determine whether it is successful

int *p=new int[1000];

if (null==p)

{

Memory allocation failure

}

2) free memory required to set a null pointer

Summary

1. Use new to request memory, use Delete to free memory

2, memory application needs to determine whether the success, free memory needs to set a null pointer

3, new and delete need to use matching

C + + Learning series One

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.