c ++代码约定 [英] c++ code conventions

查看:79
本文介绍了c ++代码约定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始学习c ++语言,当我打开ysm代码时(例如),有像szName或ptrName,iName等惯例。我知道那是什么sz - null终止string,ptr - 指针,i - 整数。在另一个代码中有带参数的函数,每个参数都在不同的行 - 我知道它是cpp惯例但我不知道,从哪里学习。我想学习c ++编码约定,但我找不到一些可以学习的资源。你推荐我一些书或资源吗?



为什么c ++为数据类型使用如此多的别名,如wchar_T或uint8,...?



在大型源代码中使用了很多宏,为什么?



对不起我的英语和这个新手问题,但我只知道C#而且没有陷阱。

I have started to learn c++ language and when i opened ysm code (for example), there was conventions like "szName" or "ptrName", "iName", etc. I know what is that "sz" - null terminated string, "ptr" - pointer, "i" - integer. In another code were functions with parameters and every parametr was on separate line - i know that it is cpp convention but i don´t know, where to learn from. I want to learn c++ coding conventions but i cant found some resources to learn from. Do you recommend me some book or resource?

why c++ using so much alias for data types like wchar_T or uint8, ... ?

in large source codes are used a lot of macros, why?

Sorry for my english and this newbie qustions, but i know only C# and there are no pitfalls.

推荐答案

没有像唯一的代码约定这样的东西。

您的团队/公司可能/可能不会决定某些代码约定。

如果没有给出明确的约定,任何人都做任何他认为正确的方式(或根本不关心......)。



命名约定,布局等是非常有争议。定制一个约定并遵循它(无论哪个)。

使用宏通常是C编码的遗留问题。 C ++假设你使用了很少的宏。

关于别名 - 你可能指的是typedef:这在使用模板时非常方便。使用模板时通常有很多typedef,如果不使用模板则通常很少。

如果你有C代码,那么你可能会再次有很多typedef:C ++隐式地使用它来键入一个类/结构class / struct name,其中C不这样做 - 如果你想访问一个结构而不是一直使用关键字 struct 来为它添加前缀,你必须明确地这样做。 br />


从你写的,我假设你主要是读C代码而不是C ++代码,对吗?



干杯

Andi
There is not such thing like "the one and only code convention".
Your team/company may/may not decide on some code convention.
If no explicit convention is given, anyone does whatever he thinks is the right way (or does not care at all...).

Naming conventions, layouting, etc. is very much debatable. Settle on one convention and follow it (no matter which).
Using macros is usually a left-over of C coding. C++ assumes you get away with little macros.
Regarding "alias" - you probably mean typedefs: that''s very convenient when working with templates. You usually have many typedefs when working with templates, and very little if not working with templates.
If you have C code, then you might have many typedefs again: C++ implicitly typedefs a class/struct with its class/struct name where as C does not do that - you have to do that explicitly if you want to access a struct without prefixing it all the time with the keyword struct.

From what you write, I assume you are mainly reading C code and not C++ code, correct?

Cheers
Andi


关于编码惯例有很多意见。如果你问10个人是什么意思,你会得到11个意见。



你用1/2/3字母小写前缀引用的风格叫做匈牙利语符号(问谷歌)并且在早期最受微软代码欢迎。它在大多数情况下已经失宠了,特别是当类或结构实例不容易找到前缀时。



我喜欢编码约定的书是 C ++编码标准 [ ^ ],由Herb Sutter和Andrei Alexandrescu(他们反对匈牙利表示法)。



无论如何,我仍然使用匈牙利语的一些功能,例如成员变量的m_前缀和其他几个工件。我发现它使代码更易于开发和维护。
There are a lot of opinions about coding conventions. If you ask 10 people what it is about, you will get 11 opinions.

The style you cite with a 1/2/3 letter lowercase prefix is called "Hungarian notation" (ask Google) and was most popular with Microsoft code in the early years. It has fallen out of favor for the most part, especially when prefixes can''t easily be found for class or struct instances.

A book I like on coding conventions is C++ Coding Standards[^], by Herb Sutter and Andrei Alexandrescu (who are opposed to Hungarian notation).

Regardless, I still use some of the features of Hungarian, such as "m_" prefix for member variables and several other artifacts. I find it makes the code easier to develop and maintain.


不要将匈牙利符号与C ++代码约定混淆。我不知道任何正式的C ++代码约定。这是一个自由的世界,每个人都应该选择他们认为最合适的东西。



为什么选择wchar_t或uint8_t?简单地表示类型的_t后缀是C89中引入的约定。我不知道他们为什么这样做?真的有必要吗?不,但我们有它,就是这样。你可以问自己为什么int而不是像Pascal那样的整数。这就是调用C ++类型的方式,就是这样。



宏是C / C ++中的一个强大功能。它们可以在小型和大型项目中使用相同的好处。我想,使用宏的一个重要原因是减少代码,并且可能提高可读性。实际上是您编写的代码,因为预处理器在编译之前会扩展它们,因此实际代码会变大。
Don''t confuse Hungarian notation with C++ code conventions. I''m not aware of any official C++ code conventions. It''s a free world, and everybody should pick what they consider most appropriate.

Why wchar_t or uint8_t? The _t suffix that simply denotes "type" was a convention introduced in C89. I don''t know why they did it? Is it really necessary? No, but there we have it and that''s it. You can ask yourself why "int" and not "integer" like in Pascal. That''s how the C++ types are called and that''s it.

Macros are a powerful feature in C/C++. They can be used with same benefits in both small and large projects. One important reason, I guess, for using macros is reducing code, and perhaps increasing readability. Actually the code that you write, because the pre-processor expands them before compiling, and therefore the actual code gets bigger.


这篇关于c ++代码约定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆