Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C++ Modifier Types

C++ Modifier Types

C++ allows placing modifiers before char, int, and double data types.

Modifiers are keywords used to change the behavior of variable types, better meeting the needs of various situations.

The following lists the data type modifiers:

Modifiers signed, unsigned, long, and short can be applied to integers,signed and unsigned can be applied to character types,long can be applied to double-precision types.

These modifiers can also be combined; the modifier signed and unsigned can also serve as a prefix for long or short modifiers. For example:unsigned long int.

C++ allows using shorthand notation to declareunsigned short integerorunsigned long integer. You can omit int and just write the word unsigned, short or long, int is implied. For example, the following two statements both declare unsigned integer variables.



signed int num1 = -10; // 定义有符号整型变量 num1,初始值为 -10

unsigned int num2 = 20; // 定义无符号整型变量 num2,初始值为 20



short int num1 = 10; // 定义短整型变量 num1,初始值为 10

long int num2 = 100000; // 定义长整型变量 num2,初始值为 100000



long long int num1 = 10000000000; // 定义长长整型变量 num1,初始值为 10000000000



float num1 = 3.14f; // 定义单精度浮点数变量 num1,初始值为 3.14

double num2 = 2.71828; // 定义双精度浮点数变量 num2,初始值为 2.71828



bool flag = true; // 定义布尔类型变量 flag,初始值为 true



char ch1 = 'a'; // 定义字符类型变量 ch1,初始值为 'a'

wchar_t ch2 = L'你'; // 定义宽字符类型变量 ch2,初始值为 '你'

To understand the difference between how C++ interprets signed and unsigned integer modifiers, let's run the following short program:

Example

#include

<

iostream

using

namespace

std

;

/*

*/

int

main

(

)

{

short

int

i

;

// Signed short integer

short

unsigned

int

j

;

// Unsigned short integer

j

=

50000

;

i

=

j

;

cout

<<

i

<<

"

"

<<

j

;

return

0

;

}

When the above program runs, it will output the following result:



-15536 50000

In the above result, the unsigned short integer 50,000's bit pattern is interpreted as the signed short integer -15,536.

Type qualifiers in C++

Type qualifiers provide additional information about variables; they are keywords used to change the default behavior of variables or functions when they are defined.

Qualifier Meaning
const const defines a constant, indicating that the variable's value cannot be modified.
volatile Modifiers volatile tells that the variable's value may be changed by factors outside the program, such as hardware or other threads.
restrict modified by restrict pointers are the only way to access the object they point to. Only C99 added the new type qualifier restrict.
mutable mutable is used to modify class member variables. Member variables modified by mutable can be modified even if the object they belong to is const.
static is used to define static variables, indicating that the variable's scope is limited to the current file or current function and will not be accessed by other files or functions.
register is used to define register variables, indicating that the variable is frequently used and can be stored in the CPU's registers to improve the program's execution efficiency.

was marked deprecated in C++11 and formally removed in C++17.

const example

const int NUM = 10; // 定义常量 NUM,其值不可修改

const int* ptr = &NUM; // 定义指向常量的指针,指针所指的值不可修改

int const* ptr2 = &NUM; // 和上面一行等价

volatile example

volatile int num = 20; // 定义变量 num,其值可能会在未知的时间被改变

mutable example



class Example {

public:

    int get_value() const {

        return value_; // const 关键字表示该成员函数不会修改对象中的数据成员

    }

    void set_value(int value) const {

        value_ = value; // mutable 关键字允许在 const 成员函数中修改成员变量

    }

private:

    mutable int value_;

};

static example



void example_function() {

    static int count = 0; // static 关键字使变量 count 存储在程序生命周期内都存在

    count++;

}

register example



void example_function(register int num) {

    // suggest compiler store variable num in a register

    // To improve program execution speed

    // But actually whether it will be stored in a register is decided by the compiler

}

Changes to register after C++11: