C ++의 함수 이름 : 대문자 사용 여부?
C ++에서 함수 이름 지정 규칙은 무엇입니까?
저는 Java 환경에서 왔으므로 일반적으로 다음과 같이 이름을 지정합니다.
myFunction(...) {
}
C ++에서 혼합 코드를 보았습니다.
myFunction(....)
MyFunction(....)
Myfunction(....)
올바른 방법은 무엇입니까?
또한, 클래스 메소드와 비 클래스 메소드에 대해 동일합니까?
'올바른 길'은 없습니다. 몇 가지 규칙이 있지만 모두 구문 론적으로 정확합니다. Google 스타일 가이드를 따를 수 있지만 다른 사람들도 있습니다.
말한 가이드에서 :
일반 함수에는 대소 문자가 혼합되어 있습니다. 접근 자와 뮤 테이터는 변수 이름과 일치합니다 : MyExcitingFunction (), MyExcitingMethod (), my_exciting_member_variable (), set_my_exciting_member_variable ().
C ++ 11 이후로 snake_case
또는 camelCase
함수 이름 에 사용할 수 있습니다 .
이는 클래스가 범위 기반 for-loop 에서 범위 표현식으로 작동하도록하려면 해당 클래스에 대해 및 (대소 문자 구분) 함수 를 정의 해야하기 때문입니다.begin
end
결과적으로 PascalCase
함수 이름에 예 를 사용 하면 범위 기반 for로 클래스 작업을 수행해야하는 경우 프로젝트의 이름 일관성을 깨야합니다.
내가 본 대부분의 코드는 camelCase
함수 (첫 글자 소문자), ProperCase/PascalCase
클래스 이름, (가장 일반적으로) snake_case
변수입니다.
하지만 솔직히 말해서 이것은 모두 지침 일뿐입니다. 가장 중요한 것은 코드베이스 전체에서 일관성을 유지하는 것입니다. 자연스럽고 효과가있는 것을 골라서 고수하십시오. 진행중인 프로젝트에 참여하는 경우 표준을 따르십시오.
프로덕션 코드에서 볼 수있는 가장 일반적인 것은 다음과 같습니다 (이 순서대로).
myFunctionName // lower camel case
MyFunctionName // upper camel case
my_function_name // K & R ?
프로그래머가 C ++ 코드에서 사용하는 명명 규칙은 일반적으로 프로그래밍 배경과 관련이 있습니다.
예를 들어 전 자바 프로그래머는 함수에 대해 소문자 낙타 대소 문자를 사용하는 경향이 있습니다.
표준 라이브러리 를 살펴보면 패턴은 일반적으로 my_function 이지만 모든 사람은 자신의 방식을 가지고있는 것 같습니다.
개인적으로는 선호 thisStyle
에 ThisStyle
기능. 이것은 정말로 개인적인 취향을위한 것입니다. 아마도 Java의 영향을 받았을 것입니다.하지만 저는 다르게 보이는 함수와 클래스를 아주 좋아합니다.
그래도 논쟁을해야한다면 그 차이는 단순한 미적 이상이라고 말하고 싶습니다. 임시 기능 스타일의 구성을 발견 할 때 약간의 생각을 덜어줍니다. 이에 반하여 Foo(1,2,3)
함수 호출 인지 아닌지 는 실제로 중요하지 않다고 주장 할 수 있습니다 . 생성자 인 경우 어쨌든 값으로 Foo를 반환하는 함수와 똑같이 작동합니다.
이 규칙은 C에 별도의 태그 네임 스페이스가 있기 때문에 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.
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:
- GNU Coding Standards (mostly C, but mentions C++)
- Google C++ Style Guide
- C++ Coding Standards: 101 Rules, Guidelines, and Best Practices
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"!)
I think its a matter of preference, although i prefer myFunction(...)
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.
Also, see the FAQ entry by Bjarne Stroustrup.
Do as you wish, as long as your are consistent among your dev. group. every few years the conventions changes..... (remmeber nIntVAr)...
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.
Really it is just dictated by the code your working in. Or, if it's your own project, your own personal preference then.
It all depends on your definition of correct. There are many ways in which you can evaluate your coding style. Readability is an important one (for me). That is why I would use the my_function
way of writing function names and variable names.
참고URL : https://stackoverflow.com/questions/1776291/function-names-in-c-capitalize-or-not
'Program Tip' 카테고리의 다른 글
SharedPreferences를 사용하여 문자열 세트를 저장하려고 할 때의 오작동 (0) | 2020.11.26 |
---|---|
Java에서 매개 변수로 함수 전달 (0) | 2020.11.26 |
C # 일반 사전에서 값 필터링 (0) | 2020.11.26 |
emacs에게 C ++ 모드에서 .h 파일을 열도록 지시하는 방법은 무엇입니까? (0) | 2020.11.26 |
jQuery "읽기 전용 아님"선택기 (0) | 2020.11.26 |