Define,const,static Usage Summary _c language

Source: Internet
Author: User
Tags modifier

1, define usage:
Define is mainly defined with macro constants, making the program look more concise, easy to maintain code, #define定义的实质只是一个常数的名字, no specific data types, no allocated memory space. The compiler is replaced with the constant when compiling. Each time you use the macro definition, you compile and allocate space, and if you use define-defined data more than once in a program, there will be multiple copies. This is done to improve the readability of the program, but the security is relatively close.

2. Const USAGE:
The global data variable defined by const has the same basic function as define, but it adds many functions on the basis of define. const defined data before the program started in the global variable area allocated space, in the process of execution, if the use of this data, direct reading can be, do not need to compile every time, the entire program is only a copy of the process. There are many things about const usage, such as:

(1) Defining constants
const int a=100; Define a as a global data region constant
const INT *a=&i; Defines a pointer to the constant I, where *a cannot be modified
int * Const a=&i; Defines a constant pointer in which a is not modifiable
const INT * Const a=&i; Defines a constant type pointer to constant I

(2) const modifier function parameters (including transfer value, address, reference)
void Fun (const int a); The modifier passes the value, but this usage is useless, because a itself is to pass in a copy of the data, is the other allocated memory, so the change to a, has no effect on the original data
void Fun (const int *a); Modify the address, to pass in the data is a location, if the program to modify the *a, then the original data will also be modified, so if you do not want to change the value of the original data, just want to reference the data in the function, you need to add a const
void Fun (const int &a); Cosmetic references, whose utility and address are the same, the reference is an alias to the data to be passed in.

With regard to cosmetic references, the following highlights:

When you enter a normal data type, you do not need to add a const modifier, because the parameter itself is a copy that is temporarily allocated to the stack space, but if the parameter is a user-defined type or class, you need to pass the reference, because you can improve efficiency.

void Fun (a); A is a user-defined type that is inefficient, and when a function body produces a type a temporary object to copy parameter A, the construction, replication, and destructor of the temporary object will consume time.

void Fun (const a& A); This is efficient in usage, and reference delivery does not require a temporary object, which saves time for the construction, replication, and destructor of temporary objects. But a reference to light may change a, so add a const.

Copy Code code as follows:

#include <iostream>
#include <string>
using namespace Std;

Class Person {
Public:
    person ()
    {
         cout<< "creat person" <<endl;
   }
    ~person ()
    {
        cout< < "destroy Person" <<endl;
   }
    virtual void fun () const
    {
         cout<< "Hello person" <<endl;
   }
};

Class Student:public Person {
Public
Student ()
{
cout<< "Create student" <<endl;
}
~student ()
{
cout<< "Desotry student" <<endl;
}
virtual void fun () const
{
cout<< "Hello sudent" <<endl;
}
};

BOOL Studentval (Student p)
{
P.fun ();
return true;
}
int main (int argc,char *argv[])
{
Student PA;
cout<<endl;
Studentval (PA);
cout<<endl;
return 0;
}


Analysis: First declare the student PA when the two constructors (student and person), and then invoke the Studentval (PA) function, you need to create a temporary variable of PA, that is, two copies of the copy constructor (student and person) are called, However, after the function is finished, the temporary variable created is destroyed, the two destructors are called, and the two constructors are called after the main function is finished. A total of 8 functions were called. If the reference is passed, and the function is changed to:
Copy Code code as follows:

BOOL Studentval (const student& p)
{
P.fun ();
return true;
}

Because the reference is passed without constructing a temporary variable, there is no need for additional construction and destructor, just 4 calls to the entire Function procedure.

In addition, the Const modifier reference can solve the "cut" problem in polymorphism, such as the implementation of polymorphism in the following code:

Copy Code code as follows:

#include <iostream>
#include <string>
using namespace Std;

Class Person {
Public:
    person ()
    {
         cout<< "creat person" <<endl;
   }
    ~person ()
    {
        cout< < "destroy Person" <<endl;
   }
    virtual void fun () const
    {
         cout<< "Hello person" <<endl;
   }
};

Class Student:public Person {
Public
Student ()
{
cout<< "Create student" <<endl;
}
~student ()
{
cout<< "Desotry student" <<endl;
}
virtual void Fun () const//Don't lose const
{
cout<< "Hello sudent" <<endl;
}
};

BOOL Studentval (person P)
{
P.fun ();
return true;
}
int main (int argc,char *argv[])
{
Student PA;
cout<<endl;
Studentval (PA);
cout<<endl;
return 0;
}


The call to Studentval (person p), when passed into the student type, displays "Hello Stuent" according to the contents of the student that should be displayed by polymorphism, but the result is "Hello person", which means it is cut off, If you change to bool Studentval (the const person &p), the problem is resolved.

(3) Const modified member function
void Fun (int a) const
(4) const modifier function return value
const int *fun (int a)

3. Static usage:
A variable that is defined inside a function, when the program executes to its definition, the compiler allocates space for it on the stack, and as you know, the space the function allocates on the stack is released at the end of the function execution, which creates a problem: how does it work if you want to save the value of this variable in a function to the next call? The easiest way to think about it is to define a global variable, but there are many drawbacks to defining it as a global variable, the most obvious disadvantage being that it destroys the scope of the variable's access (so that the variables defined in this function are not just controlled by this function). Therefore, in C + +, the static variable is introduced to modify the variable, it can instruct the compiler to save the variable in the static storage of the program, so that the purpose is achieved, and the access scope of this variable is unchanged.

For local variables, static changes the way in which the variable is stored so that it becomes static, connected by an internal connection (which can only be used in the file, and local variables are inherently connected), that is, the local variable only changes the way it is stored, and does not change the way it is connected. For global variables, the storage mode is not changed (the global variable is already statically stored), it only changes its connection type, the global variable by default is external, that can be directly used by other external files, only in advance declaration extern, if added static, only in this file, That is, the global variable only changes the connection mode and does not change the storage mode.

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.