Home >Backend Development >C++ >The role of extern, static and mutable in C++ function declarations: understanding their semantics and effects

The role of extern, static and mutable in C++ function declarations: understanding their semantics and effects

PHPz
PHPzOriginal
2024-05-01 17:09:02344browse

The keyword function extern refers to functions in other source files static limits the scope of the function to the current source file mutable allows objects declared as const to be modified within the function

C++ 函数声明中 extern、static 和 mutable 的角色:理解它们的语义和作用

C The roles of extern, static and mutable in function declarations: understanding their semantics and effects

In C, the extern, static and mutable keywords in function declarations have different semantics and effect.

extern

  • extern keyword indicates that the function is defined in other source files.
  • It allows the function to be referenced in the current source file without having to include the function's definition.
  • This is useful when there are multiple source files that make up a program.

Example:

// header.h
extern int add(int a, int b);

// source1.cpp
#include "header.h"
int main() {
  int sum = add(1, 2);
  return 0;
}

In source1.cpp, the extern keyword allows reference to the add function declared in header.h without including the add function Definition.

#static

  • The static keyword is used to limit the scope of a function.
  • Using the static keyword in the function declaration means that the function can only be used in this source file and cannot be accessed in other source files.
  • is usually used to define auxiliary functions that are only used in the current source file.

Example:

// source1.cpp
static int localFunction() {
  return 10;
}

int main() {
  int x = localFunction(); // 可以访问 localFunction
  return 0;
}

Due to the static keyword, localFunction can only be accessed in source1.cpp and not in other source files.

mutable

  • The mutable keyword is used to allow modification of a const object declared in a function declaration.
  • Declaring a const object in a function declaration usually means that the object is immutable.
  • The mutable keyword allows const objects to be modified inside a function.

Example:

// source1.cpp
class MyClass {
public:
  const int x = 10; // 不可变数据成员
  mutable int y = 20; // 可变数据成员
};

void modifyConst(MyClass& obj) {
  obj.y++; // 允许修改 y,因为 y 是 mutable
}

Due to the mutable keyword, the modifyConst function can modify the y data member of the MyClass class, even if y is const.

Understanding the semantics and effects of these keywords is critical to writing robust and efficient C programs.

The above is the detailed content of The role of extern, static and mutable in C++ function declarations: understanding their semantics and effects. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:What does i++ mean in c++Next article:What does i++ mean in c++