Define const static

  • 2020-04-02 01:48:21
  • OfStack

1. Define usage:
Define is mainly used for macro constant definition, so that the program looks more concise, easy to maintain code, the essence of #define definition is just a constant name, no specific data type, there is no allocated memory space. The constant is replaced by the compiler at compile. Each time the macro definition is used, it compiles and allocates space, and if the data defined by define is used multiple times in a program, there will be multiple copies. This is done to improve the readability of the program, but with relatively little security.

2. Usage of const:
The global data variable defined by const has the same basic function as define, but many functions are added on the basis of define. The data defined by const is allocated in the global variable area before the program starts. If the data is used during the execution of the program, it can be read directly. There is no need to compile every time, and there is only one copy in the whole process of the program. There are many USES for const, such as:

(1) define constants
Const int a = 100;   // define a as a global data area constant
A = const int * & I;     // defines a pointer to the constant I, where *a cannot be modified
* const int a = & I;   // defines a constant pointer where a cannot be modified  
Const int * const a=& I; // defines a constant pointer to the constant I

(2) parameters of const modified function (including pass value, address and reference)
Void fun (const int a);             // modifies the pass value, but this usage is useless, because a itself is to pass a copy of the data, is another memory allocation, so the change to a, the original data is not affected
Void fun (const int * a);         // modify the address, the data to be passed in is an address, at this time if the program to modify *a, 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 const
Void fun (const int & a);         // modifies a reference, which has the same effect as addressing, by giving an alias to the data to be passed in.

Here are some key points about decorated references:

When entering a normal data type, there is no need to const because the parameter itself is a copy temporarily assigned to the stack space, but when the parameter is a user-defined type or class, it needs to be passed by reference to improve efficiency.

Void fun (aa);       //A is A user-defined type, which is inefficient. When A temporary object of type A is generated in the function body, the construction, replication and destruction of the temporary object will consume time.

Void fun (const A & A);   // this usage is efficient, and the passing of references does not require the generation of temporary objects, thus saving the time consumed by the construction, replication, and destruction of temporary objects. But just using the reference might change a, so                                                                                   Add const.


#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: the first statement Student pa conducted two constructors (Student and person), then call studentval (pa) function, you need to create temporary variables, pa, which calls the copy constructor twice (Student and person), but after the end of the function, create temporary variables were destroyed and the destructor call twice, and when the end of the main function, pa destruction and invokes the constructor twice. The function was called eight times. If changed to pass by reference, and function changed to:

bool studentval(const Student& p)
{
p.fun();    
return true;
}

Since no temporary variables are constructed when the reference is passed, there is no need for additional construction and destruction, and only four calls are required for the entire function.

In addition, the const modified reference can also solve the "cut off" problem in polymorphism, such as the implementation of polymorphism in the following code:


#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 throw the 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;
}

Studentval (Person p) is supposed to be called. When passing in the Student type, according to the polymorphic content of the Student should be displayed, that is, "hello stuent" is displayed, but the result is "hello Person ", indicating that it is cut off, if changed to bool studentval(const Person &p), then the problem is solved.

(3) const modifies member functions
Void fun (int a) const
(4) return value of const modifier function
Const int * fun (int a)

3. Static:
Function within the definition of the variables in the program execution to its definition, the compiler space allocated on the stack for it, as we all know, function of the space allocated on the stack will release at the end of this function, the result is a question: if you want to be the function of the value of this variable is saved to the next call, how to do? The easiest way to think about it is to define a global variable, but defining it as a global variable has many disadvantages, the most obvious of which is that it breaks the scope of access to the variable (making the variables defined in the function more than controlled by the function). Therefore, C++ introduced the static variable static, with it to modify the variable, it can instruct the compiler to save the variable in the program's static storage allocation space, so as to achieve the purpose, but also make the variable access scope unchanged.

For local variables, static changes the storage mode of variables, making them into static storage, and the connection mode is internal connection (can only be used in the file, local variables are already internal connection), that is, local variables only change the storage mode, do not change the connection mode. For global variables, does not change the way of storage (global variable is the static storage), it just change the connection type, a global variable is outreach by default, which can be used by other external files directly, only said earlier extern, if combined with the static, can only be used in this document, namely global variables only change the mode of connection, don't change the way of storage.


Related articles: