C++ 中的函数名称:大写与否?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1776291/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Function names in C++: Capitalize or not?
提问by user69514
What's the convention for naming functions in C++?
在 C++ 中命名函数的约定是什么?
I come from the Java environment so I usually name something like:
我来自 Java 环境,所以我通常命名为:
myFunction(...) {
}
I've seen mixed code in C++,
我见过 C++ 中的混合代码,
myFunction(....)
MyFunction(....)
Myfunction(....)
What's the correct way?
正确的方法是什么?
Also, is it the same for a class method as well as a non-class method?
另外,类方法和非类方法是否相同?
采纳答案by int3
There isn't a 'correct way'. They're all syntactically correct, though there are some conventions. You could follow the Google style guide, although there are others out there.
没有“正确的方法”。尽管有一些约定,但它们在语法上都是正确的。您可以遵循Google style guide,尽管那里还有其他指南。
From said guide:
从上述指南:
Regular functions have mixed case; accessors and mutators match the name of the variable: MyExcitingFunction(), MyExcitingMethod(), my_exciting_member_variable(), set_my_exciting_member_variable().
常规函数大小写混合;访问器和修改器匹配变量的名称:MyExcitingFunction()、MyExcitingMethod()、my_exciting_member_variable()、set_my_exciting_member_variable()。
回答by emlai
Since C++11, you may want to use either snake_case
or camelCase
for function names.
从 C++11 开始,您可能希望使用snake_case
或camelCase
函数名称。
This is because to make a class work as the range-expression in a range-based for-loop, you have todefine functions called begin
and end
(case-sensitive) for that class.
这是因为要使类在基于范围的 for-loop 中用作范围表达式,您必须为该类定义调用begin
和end
(区分大小写)的函数。
Consequently, using e.g. PascalCase
for function names means you have to break the naming consistency in your project if you ever need to make a class work with the range-based for.
因此,使用 eg PascalCase
for 函数名意味着如果您需要使类使用基于范围的 for ,则必须打破项目中的命名一致性。
回答by Donnie
Most code I've seen is camelCase
functions (lower case initial letter), and ProperCase/PascalCase
class names, and (most usually), snake_case
variables.
我见过的大多数代码都是camelCase
函数(小写首字母)和ProperCase/PascalCase
类名,以及(最常见的)snake_case
变量。
But, to be honest, this is all just guidance. The single most important thing is to be consistent across your code base. Pick what seems natural / works for you, and stick to it. If you're joining a project in progress, follow their standards.
但是,老实说,这只是指导。最重要的一点是在您的代码库中保持一致。选择看起来自然/对你有用的东西,并坚持下去。如果您要加入正在进行的项目,请遵循他们的标准。
回答by Steven Keith
The most common ones I see in production code are (in this order):
我在生产代码中看到的最常见的是(按此顺序):
myFunctionName // lower camel case
MyFunctionName // upper camel case
my_function_name // K & R ?
I find the naming convention a programmer uses in C++ code usually has something to do with their programming background.
我发现程序员在 C++ 代码中使用的命名约定通常与他们的编程背景有关。
E.g. ex-java programmers tend to use lower camel case for functions
例如,前 Java 程序员倾向于对函数使用小驼峰式大小写
回答by TofuBeer
If you look at the standard libraries the pattern generally is my_function, but every person does seem to have their own way :-/
如果您查看标准库,则模式通常是 my_function,但每个人似乎都有自己的方式:-/
回答by Steve Jessop
Personally, I prefer thisStyle
to ThisStyle
for functions. This is really for personal taste, probably Java-influenced, but I quite like functions and classes to look different.
就个人而言,我更喜欢thisStyle
到ThisStyle
的功能。这真的是出于个人喜好,可能受 Java 影响,但我非常喜欢函数和类看起来不同。
If I had to argue for it, though, I'd say that the distinction is slightly more than just aesthetic. It saves a tiny bit of thought when you come across function-style construction of a temporary. Against that, you can argue that it doesn't actually matter whether Foo(1,2,3)
is a function call or not - if it is a constructor, then it acts exactly like a function returning a Foo by value anyway.
不过,如果我不得不为它争论,我会说这种区别不仅仅是美学上的。当您遇到临时的函数式构造时,它可以省去一点思考。与此相反,您可以争辩说,是否Foo(1,2,3)
是函数调用实际上并不重要- 如果它是构造函数,那么它的行为就像一个按值返回 Foo 的函数。
The convention also avoids the function-with-same-name-as-a-class-is-not-an-error fiasco that C++ inherits because C has a separate tag namespace:
该约定还避免了 C++ 继承的 function-with-same-name-as-a-class-is-not-an-error 惨败,因为 C 有一个单独的标签命名空间:
#include <iostream>
struct Bar {
int a;
Bar() : a(0) {}
Bar(int a) : a(a) {}
};
struct Foo {
Bar b;
};
int Bar() {
return 23;
}
int main() {
Foo f;
f.b = Bar();
// outputs 23
std::cout << f.b.a << "\n";
// This line doesn't compile. The function has hidden the class.
// Bar b;
}
Bar is, after all, both a noun and a verb, so could reasonably be defined as a class in one place and a function in another. Obviously there are better ways to avoid the clash, such as proper use of namespaces. So as I say, really it's just because I prefer the look of functions with lower-case initials rather than because it's actually necessary to distinguish them from from classes.
毕竟,Bar 既是名词又是动词,因此可以合理地在一处定义为类,而在另一处定义为函数。显然有更好的方法来避免冲突,例如正确使用命名空间。所以正如我所说,实际上只是因为我更喜欢带有小写首字母的函数的外观,而不是因为实际上有必要将它们与类区分开来。
回答by Laurence Gonsalves
Unlike Java, C++ doesn't have a "standard style". Pretty much very company I've ever worked at has its own C++ coding style, and most open source projects have their own styles too. A few coding conventions you might want to look at:
与 Java 不同,C++ 没有“标准风格”。我曾经工作过的几乎所有公司都有自己的 C++ 编码风格,大多数开源项目也有自己的风格。您可能想要查看的一些编码约定:
- GNU Coding Standards(mostly C, but mentions C++)
- Google C++ Style Guide
- C++ Coding Standards: 101 Rules, Guidelines, and Best Practices
- GNU 编码标准(主要是 C,但也提到了 C++)
- 谷歌 C++ 风格指南
- C++ 编码标准:101 条规则、指南和最佳实践
It's interesting to note that C++ coding standards often specify which parts of the language not to use. For example, the Google C++ Style Guide says "We do not use C++ exceptions". Almost everywhere I've worked has prohibited certain parts of C++. (One place I worked basically said, "program in C, but new
and delete
are okay"!)
有趣的是,C++ 编码标准通常指定不使用语言的哪些部分。例如,Google C++ Style Guide 说“我们不使用 C++ 异常”。我工作过的几乎所有地方都禁止使用 C++ 的某些部分。(一个地方我的工作基本上是说,“在C程序,但new
并delete
是没关系”!)
回答by Nemanja Trifunovic
As others said, there is no such thing in C++. Having said that, I tend to use the style in which the standard library is written - K & R.
正如其他人所说,C++ 中没有这样的东西。话虽如此,我倾向于使用编写标准库的风格——K&R。
回答by sud03r
I think its a matter of preference, although i prefer myFunction(...)
我认为这是一个偏好问题,虽然我更喜欢 myFunction(...)
回答by Daniel Bingham
There isn't so much a 'correct' way for the language. It's more personal preference or what the standard is for your team. I usually use the myFunction() when I'm doing my own code. Also, a style you didn't mention that you will often see in C++ is my_function() - no caps, underscores instead of spaces.
语言没有那么多“正确”的方式。这更多是个人喜好或您的团队的标准。我通常在编写自己的代码时使用 myFunction()。此外,您在 C++ 中经常看到的一种您没有提到的样式是 my_function() - 没有大写字母、下划线而不是空格。
Really it is just dictated by the code your working in. Or, if it's your own project, your own personal preference then.
实际上,这只是由您所使用的代码决定的。或者,如果这是您自己的项目,则是您自己的个人偏好。