man/checkers/functionStatic.md
Message: The member function 'x' can be static
Category: Readability
Severity: Style
Language: C++
This checker identifies member functions that do not access any non-static member variables or call any non-static member functions. Such functions can be declared as static to indicate that they don't require an object instance to operate.
This warning helps improve code quality by:
The motivation of this checker is to improve readability.
Add the static keyword to the function declaration to indicate that it doesn't require an object instance.
Before:
class Calculator {
public:
int add(int a, int b) {
return a + b; // Only uses parameters
}
void printMessage() {
std::cout << "Hello World" << std::endl; // Uses no instance data
}
bool isValidNumber(int num) {
return num > 0 && num < 1000; // Pure function
}
};
After:
class Calculator {
public:
static int add(int a, int b) {
return a + b; // Can be called as Calculator::add(5, 3)
}
static void printMessage() {
std::cout << "Hello World" << std::endl; // Can be called without instance
}
static bool isValidNumber(int num) {
return num > 0 && num < 1000; // Clearly indicates no state dependency
}
};
functionConst - for member functions that can be declared const