Home > Backend Development > C++ > Underscore nomenclature for C++ function naming

Underscore nomenclature for C++ function naming

WBOY
Release: 2024-04-24 22:00:01
Original
532 people have browsed it

The benefits of using underscore function naming in C include: enhancing readability, avoiding name conflicts, and clarifying function usage. Syntax: identifier_function name (parameter list). Convention: A single underscore indicates a private or protected function, a double underscore indicates a static function, and a triple underscore indicates an implementation detail. For example, in the Student class, the private function get_name() can be renamed to _get_name() to distinguish it from the public function.

C++ 函数命名的下划线命名法

Underscore nomenclature for C function naming

In C, using underscore nomenclature for function naming has many benefits:

  • Readability enhancement: Underscores can separate words, making the function name easier to read and understand.
  • Avoid name conflicts: Underscore prefix prevents function names from conflicting with built-in identifiers in C.
  • Clear Purpose: Underscores can convey the specific purpose of a function, especially when functions with the same name have different behaviors.

Syntax:

标识符_函数名(参数列表)
Copy after login

Convention:

  • Prefixed by an underscore to indicate that the function is private or protected protected.
  • The two underscores in the prefix indicate that the function is static.
  • Three underscores prefixed indicate that the function is an implementation detail and should not be called from outside the class.

Practical case:

Suppose we have a Student class, which contains a class named get_name() of functions:

class Student {
public:
    std::string get_name() { return name; }

private:
    std::string name;
};
Copy after login

Using underscore nomenclature, we can rename the private function to _get_name():

class Student {
public:
    std::string get_name() { return _get_name(); }

private:
    std::string _get_name() { return name; }
};
Copy after login

In this way, we can distinguish the public get_name() function and the private _get_name() function.

Tip:

  • Consistently use underscore nomenclature to ensure code clarity.
  • When using underscores in function names, maintain appropriate spacing.
  • Avoid overusing underscores, as excessive use will reduce the readability of the code.

The above is the detailed content of Underscore nomenclature for C++ function naming. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template