C++ keyword exploration: understanding variables, function parameters, function return values, and modifiers of class member functions

In C++ programming, we often encounter keywords that can be used to modify variables, function parameters, function return values, and class member functions. These keywords includeconst, static, volatile,mutable,signed,unsigned,long strong>,short,virtual,explicit,inline, andfriend. Let’s take a closer look at these keywords.

const keyword

const is a constant modifier, which can be used to modify variables, function parameters, function return values, and class member functions.

  • Modify variable: When const modifies a variable, it means that the value of the variable cannot be modified. For example:
const int a = 10; // a is a constant and cannot be modified
  • Modify function parameters: When const modifies a function parameter, it means that the parameter cannot be modified inside the function. This is particularly useful for reference and pointer parameters, as it prevents the function from accidentally modifying the parameter’s value. For example:
void func(const int & amp; a) {<!-- -->
    // In this function, a cannot be modified
}
  • Modify function return value: When const modifies a function return value, it means that the return value cannot be modified. This is particularly useful when returning a reference or pointer, as it prevents the caller of the function from modifying the return value. For example:
const int & amp; func() {<!-- -->
    static int a = 10;
    return a; // Returns a constant reference, the caller cannot modify the value of a through this reference
}
  • Modify a member function of a class: When const modifies a member function of a class, it means that the member function cannot modify any member variables of the class (unless these variables are declared as mutable) . This is useful for operations that require the object’s state to remain unchanged. For example:
class MyClass {<!-- -->
public:
    void func() const {<!-- -->
        // This is a const member function, it cannot modify any member variables of the class
    }
};

In C++, the const keyword is used to declare a variable or function parameter as read-only, indicating that its value cannot be modified after initialization. Once a const variable is assigned a value, it cannot be changed, and any attempt to modify it will result in a compilation error.

static keyword

static is a storage class specifier, which can be used to modify global variables, local variables, class member variables and class member functions.

  • Modify global variables: When static modifies a global variable, it means that the global variable can only be used in this file. This prevents other files from accessing this global variable through external links.
static int a = 10; // a is a static global variable and can only be used in this file
  • Modify local variables: When static modifies a local variable, it means that the life cycle of this local variable is global, that is, it will always exist during the running of the program. And they will only be initialized once.
void func() {<!-- -->
    static int a = 0; // a is a static local variable, its life cycle is global
    a + + ;
    cout << a << endl;
}
  • Modify class members: When static modifies a member variable of a class, it means that the member variable is shared by all objects of the class. When static is used to modify a member function of a class, it means that the member function can be called without relying on any object.
class MyClass {<!-- -->
public:
    static int a; // a is a static member variable, all MyClass objects share this variable

    static void func() {<!-- -->
        // This is a static member function, it can be called without relying on any object
    }
};

Note: The static keyword is only used to declare static members in the class definition, not the definition of the static member. For example:

class X {<!-- -->
    static int n;
}; // Statement (use 'static')

int X::n = 1; // Definition (do not use 'static')

Here, n is a static member of class X.

To sum up, although **const ** and **static ** have some minor differences in syntax, they can both help us write safer, easier to understand and maintain code.

volatile keyword

When we use the volatile keyword to modify a variable, it tells the compiler that this variable may be changed unexpectedly, so the compiler should not optimize this variable. This is useful when dealing with special cases such as multi-threading or hardware interaction.

volatile int flag = 0;

In this example, we tell the compiler that the flag variable may be changed unexpectedly, so the compiler should not optimize it.

mutable keyword

When we use the mutable keyword to modify a member variable of a class, we can modify the mutable member even in a const member function value.

class MyClass {<!-- -->
public:
    mutable int a;

    void func() const {<!-- -->
        a = 10; // Even in const member functions, we can modify the value of mutable members
    }
};

In this example, we define a mutable member a. Even in the const member function func(), we can modify the value of a.

signed and unsigned keywords

When we use the signed and unsigned keywords to modify an integer variable, they indicate whether the variable is signed or unsigned respectively.

signed int a = -10; // a is a signed integer
unsigned int b = 10; // b is an unsigned integer

In this example, we define a signed integer a and an unsigned integer b.

long and short keywords

When we use the long and short keywords to modify an integer or floating-point type, they mean to increase or decrease the length of this type respectively.

long long a = 10000000000LL; // a is a long integer
short b = 10; // b is a short integer

In this example, we define a long a and a short b.

virtual keyword

When we use the virtual keyword to modify a member function of a class, it indicates that the function is a virtual function and can be overridden by subclasses.

class Base {<!-- -->
public:
    virtual void func() {<!-- -->
        cout << "Base::func()" << endl;
    }
};

class Derived : public Base {<!-- -->
public:
    void func() override {<!-- -->
        cout << "Derived::func()" << endl;
    }
};

The virtual keyword tells the compiler that a non-static member function is virtual and supports dynamic dispatch.
It can only appear in the initial declaration of a non-static member function (i.e. when declared in a class definition).
Virtual functions are member functions whose behavior can be overridden in a derived class.
In contrast to non-virtual functions, the overriding behavior is preserved even if there is no compile-time information about the actual type of the class

In this example, the member function func() of the base class Base is declared as a virtual function. Therefore, in the derived class Derived, we can override this function.

explicit keyword

When we modify a constructor with the explicit keyword, it prevents the compiler from performing unexpected type conversions. In C++, if a class has a constructor that accepts a single parameter, then this constructor may be used by the compiler as an implicit type conversion. This conversion may cause some unexpected results. For example:

class MyClass {<!-- -->
public:
    MyClass(int a) {<!-- -->
        // ...
    }
};

void func(MyClass obj) {<!-- -->
    // ...
}

int main() {<!-- -->
    func(10); // Implicit type conversion
    return 0;
}

In this example, we define a class MyClass, which has a constructor that accepts a single parameter. Then, we define a function func, which accepts a MyClass object as a parameter. In the main function, we call the func function and pass in an integer. Since MyClass has a constructor that accepts an integer, the compiler automatically converts this integer to a MyClass object. This is called implicit type conversion.

However, in some cases, we may not want the compiler to perform this implicit type conversion, as it may lead to some unexpected results. At this time, we can use the explicit keyword to modify the constructor to prevent the compiler from performing implicit type conversion. For example:

class MyClass {<!-- -->
public:
    explicit MyClass(int a) {<!-- -->
        // ...
    }
};

void func(MyClass obj) {<!-- -->
    // ...
}

int main() {<!-- -->
    func(10); // Compile error: cannot convert int to MyClass
    func(MyClass(10)); // Correct: explicit type conversion
    return 0;
}

In this example, because the constructor is declared as explicit, the compiler does not automatically convert an integer to MyClass.
To sum up, using the **explicit ** keyword can prevent the compiler from performing unexpected implicit type conversions, thus making our code safer and easier to understand.

inline keyword

When we use the inline keyword to modify a function, it suggests the compiler to insert the function body directly into each call to reduce the overhead of function calls.

inline int add(int a, int b) {<!-- -->
    return a + b;
}

In this example, we recommend that the compiler insert the body of the add function directly into each call (a process called expansion) and then compile the entire code. If the function is small, using inline functions may increase efficiency because they eliminate the overhead associated with function calls.

friend keyword

When we use the friend keyword to modify a non-member function or other class, it allows this function or class to access private and protected members of the current class.

class MyClass {<!-- -->
private:
    int a;

    friend void func(MyClass & amp; obj);
};

void func(MyClass & amp; obj) {<!-- -->
    obj.a = 10; // Can access private members of MyClass
}

In this example, we allow the non-member function func to access the private members of MyClass.
In other words, the friend keyword can be used in C++ to share previously hidden class information. For example, a private member of a class is hidden from all other classes unless modified by a getter or setter. However, we can declare a friend class to access and modify these private and protected members.

To sum up, keywords in C++ provide rich language features, allowing us to write safer, easier to understand and maintain code.