C++ basic variables

C++ basic variables

variable

The basic form of the variable is: first is the type specifier, followed by a list of one or more variable names, which contain multiple variable names separated by commas, and finally ended with a semicolon.

int sum = 0;
string first_name,last_name;

Variable naming conventions: (1) Variable names are generally lowercase letters (2) Customized class names are generally capitalized (3) If the variable name consists of multiple words, it is better to have a clear distinction between the words

Quote

A reference is another name for an object. All references are not objects. It is just another name for an existing object. The reference type is defined by declaring it as &.

    Student student("baxiang",26);
    Student &refStudent = student;
    student.show();
    refStudent.show();

The variable declared as a reference must be initialized, but the initialized value must be an object.

 string &rName = "test1";//error

Generally, during initialization, the initialization value will be copied to the newly created object. The reference object and the referenced object are initialized together, instead of copying a new initialization to the reference, that is, the reference shares a memory with the referenced object. address.

    string name = "test";
    string &rName = name;
    string &othName = rName;

pointer

Similar to references, pointers also implement indirect access to other objects. The way to define pointers is to put an asterisk * in front of the variable;

int *value,*result;
float *height;

The pointer stores the address information of the object. If you want to get the address of the object, you need to use the address character &

string test = "test";
string *testPoint = &test;

If the pointer points to an object, you need to dereference the symbol (*) to access the currently pointed object

 cout << *testPoint<<endl;

const qualifier

We sometimes hope that the value of the variable we define cannot be modified. In order to meet this requirement, we can use the keyword const to restrict the variable type

    const int buffSize = 1024;
    buffSize = 512;//error, unable to modify 

When the program is compiled, the const variable will be replaced with the corresponding value wherever the variable is used, that is, the compiler will find the place where the buffSize is used and replace it with 1024. By default, const variables are set to be valid only in files. When const variables with the same name appear in multiple files, independent const variables are actually defined in different files. If you need to implement file sharing, in this case we don't want the compiler to generate independent variables for each file, that is, declare that other files can be used in one file. The solution is to add the keyword extern to the const variable whether it is declared or defined.

extern const int buffSize = 1024//initialize a constant in file_1.cpp
extern const int buffSize//Only constants can be defined in file_1.h

static member

Static data members are preceded by the keyword static when they are defined or explained. Static member initialization is different from general data member initialization. The format of static data member initialization is as follows:

     <data type><class name>::<static data member name>=<value>
Reference: https://cloud.tencent.com/developer/article/1437083 C++ Basic Variables-Cloud + Community-Tencent Cloud