Standard C + + (1)

Source: Internet
Author: User

First, reference

A reference is an alias for a variable (the target), and the action on the reference is exactly the same as the direct operation on the variable.

Declarative method of Reference: type identifier & reference name = target variable name;

Example: int& num;

References are similar to aliases

Attention:

(1) & Here is not the address calculation, but the role of identification.

(2) The type identifier refers to the type of the target variable.

(3) When declaring a reference, it must be initialized at the same time.

(4) When the declaration is complete, the target variable name is equivalent to two names, that is, the target name and reference name, and the reference name cannot be used as an alias for the other variable names.

(5) A reference can also be used as the return value of a function, but must not return a reference to a local variable.

(6) A reference can be used as an argument to a function, and the object it refers to is the argument to the function. A reference can achieve the effect of a pointer.

(7) References can refer not only to identifiers, but also to immediate numbers, but must have the Const property

II. Memory management operators for C + +

Using New/delete in C + + to manage memory, new is used to request memory, and delete is used to free memory.

The specific usage is as follows:

 

#incldue <iostream>class  student{    char name[];     Char sex;      Short ;}; int Main () {       new  Student;        Delete Stu;}

When you create a struct object using new, the constructor is automatically called, and the destructor is called automatically when delete
"Example" student* stu = new Student ("hehe");

[] You can request or release an array with new and delete. New[] cannot be mixed with Delete, must be released using delete[]

  int New int [Ten] {1,2,3,4,5,6,7,8,9, Ten  };  Delete[] stu;
Iii. functions of C + +

Functions in C + + can be executed before the main function

Features of C + + with function overloading

You can use the same function name to define functions that have different parameters (the return value can be different, assuming the parameters are different).

The parameters of a function in C + + can have a default value,
1. The default parameter of the function must be on the right.
2, the default parameters should be used sparingly, because it may cause overloading errors
3. The default parameter is the compiler help assignment at compile time, so the parameter can only be assigned by global variable, static variable and literal value.

int func (int,char"abcd") {}void func (char ch,short  num) {}
Four, the name space

C + + refers to the mechanism of namespaces.
Namespaces: Further segmentation of the global namespace. You can create separate namespaces to prevent naming conflicts between each other

1. Definition of namespaces

        namespace name        {            // variable;            // function;            // structure, class;        }

2. Merging namespaces
A, namespaces with the same name are automatically merged
B. Identifiers must be unique in the same namespace

3. Separation of definitions and declarations

        namespace N1        {            // declaring a function            in the namespace void func (void);        }         // define a function        outside of a namespace void n1::func (void) {}

4. How to use content in namespaces
A, Domain qualifier (::), directly used, namespace:: identifier. "Example" Ni::func ();
B, using namespace name space; function is to put identifiers in namespaces
C, using namespace:: identifier, which indicates that this identifier is exposed to subsequent code.

5. Nesting of namespaces
Namespaces can be nested, but are parsed on a per-layer basis when used. N1::n2::n3::num

        namespace N1        {            namespace  n2            {                namespace  n3                {                                    }         }}

6. Global space attribution to anonymous space
Identifiers defined in the global space belong to this anonymous space, and anonymous spaces are open by default.
If there is an identifier in the function that has the same name as a mask in the anonymous space, you can use an empty domain qualifier to represent him:: Variable name

Standard C + + (1)

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.