如何协调常用的C

如何协调常用的C

本文介绍了如何协调常用的C ++命名约定与库的命名约定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大多数C ++命名惯例规定使用camelCaseIdentifiers:以字母和变量(getPrice(),isValid(),largestValue)的小写字母开头的类(Person,Booking) 。这些建议与C ++库的命名约定完全不一致,C ++库涉及类(string,set,map,fstream)的小写名称和方法和字段的names_joined_with_an_underscore(find_first_of,lower_bound,reverse_iterator,first_type)。进一步复杂的图片是操作系统和C库函数,涉及C和Unix中的压缩小写名称,以及Windows中大写字母开头的函数。



代码是一团糟,因为一些标识符使用C ++库,C或操作系统命名约定,而其他标识符使用规定的C ++约定。编写类库或函数库的方法是痛苦的,因为一个类似的东西以不同样式的名字结束。



那么,你如何协调这些不同的命名约定?

解决方案

采用C ++ naming_convention



我慢慢地看到这些约定转移到生产代码,但它是一个反对MFC命名约定的战斗,在许多地方仍然盛行。 / p>

与旧标准对抗的其他风格差异使用尾随下划线,而不是 m _ 来表示成员。

Most C++ naming conventions dictate the use of camelCaseIdentifiers: names that start with an uppercase letter for classes (Person, Booking) and names that start with a lowercase letter for fields and variables (getPrice(), isValid(), largestValue). These recommendations are completely at odds with the naming conventions of the C++ library, which involve lowercase names for classes (string, set, map, fstream) and names_joined_with_an_underscore for methods and fields (find_first_of, lower_bound, reverse_iterator, first_type). Further complicating the picture are operating system and C library functions, which involve compressed lowercase names in C and Unix and functions starting with an uppercase letter in Windows.

As a result my code is a mess, because some identifiers use the C++ library, C, or operating system naming convention, and others use the prescribed C++ convention. Writing classes or methods that wrap functionality of the library is painful, because one ends with different-style names for similar things.

So, how do you reconcile these disparate naming conventions?

解决方案

One way it to adopt the C++ naming_convention, this is what most code examples in the literature do nowadays.

I slowly see these conventions move into production code but it's a battle against MFC naming conventions that still prevail in many places.

Other style differences that fight against old standards are using trailing underscores rather than m_ to denote members.

这篇关于如何协调常用的C ++命名约定与库的命名约定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 03:39