简体   繁体   English

使用C ++的Win32 / MFC的命名约定

[英]Naming convention for Win32/MFC with C++

I am working on an application which uses Win32/MFC framework and C++ programming language using Visual Studio 2010. 我正在使用Visual Studio 2010使用Win32 / MFC框架和C ++编程语言的应用程序。

My question is which naming convention needs to use for variables, function name, class name and etc. I heard Microsoft suggest to use “Hungarian notation” naming convention. 我的问题是变量,函数名,类名等应使用哪种命名约定。我听说微软建议使用“匈牙利表示法”命名约定。

Can you please let me know which standard to use? 您能告诉我使用哪种标准吗?

You might have a truly good reason to want your code to look familiar to other programmers who have learned MFC. 可能确实有充分的理由希望您的代码对学习过MFC的其他程序员看起来很熟悉。 If so, then you could emulate the conventions of the MFC Samples and Tutorials , and do what they do... 如果是这样,那么您可以效仿MFC Samples and Tutorials的约定,并执行它们的工作...

But MFC is a legacy library which originated very early, and times have changed. 但是MFC是最早起源的旧式库,并且时代已经改变。 Microsoft's style guidelines now specifically say NOT to use Hungarian notation: 现在,Microsoft的样式准则明确规定不要使用匈牙利表示法:

http://msdn.microsoft.com/en-us/library/ms229042.aspx http://msdn.microsoft.com/zh-CN/library/ms229042.aspx

That aside, rather than blindly following any particular document, it is probably better to evaluate each individual convention. 此外,与其盲目地遵循任何特定文件,不如评估每个单独的公约可能更好。 For instance, when MFC samples make classes and have member variables, they start the name with m_ . 例如,当MFC示例创建类并具有成员变量时,它们以m_开头。 StackOverflow has questions about this particular idea and you can read about alternatives: StackOverflow对此特定想法有疑问,您可以阅读以下替代方法:

Why use prefixes on member variables in C++ classes 为什么在C ++类的成员变量上使用前缀

I'm more aligned with Qt's API Style Guidelines, and most of their conventions work for me: 我更了解Qt的API样式指南,并且大多数约定对我有用:

http://doc.qt.nokia.com/qq/qq13-apis.html http://doc.qt.nokia.com/qq/qq13-apis.html

Speaking of which, Qt is a nice C++ offering that has continued to evolve and mature. 说到这,Qt是一个不错的C ++产品,并且一直在发展和成熟。 Blows MFC out of the water (completely) AND is cross-platform... 将MFC吹出水面(完全),并且是跨平台的...

http://qt.nokia.com/products/developer-tools/ http://qt.nokia.com/products/developer-tools/

If you want to be locked in the Microsoft universe go for .NET and C#. 如果要锁定在Microsoft Universe中,请使用.NET和C#。 That would be a step up, and you wouldn't be stuck with the MFC abandonware. 这将是一步,而且您不会被MFC放弃软件所困扰。

Oh boy. 好家伙。 That's almost like asking people what religion you should follow. 这几乎就像问别人应该遵循哪种宗教。 At least you didn't ask what text editor you should use... 至少您没有问过应该使用哪种文本编辑器...

My 2c: do what works for you. 我的2c:做对自己有用的事情。 Also consider whether you are just coding for yourself, or whether you expect other people to read or work on the code you write, and whether you are starting a project from scratch, or building on existing code. 还应考虑是否只是为自己编写代码,还是希望其他人阅读或使用编写的代码,是否从头开始一个项目,还是建立在现有代码上。

Naming convention really doesn't matter much, so long as: 只要以下条件,命名约定实际上并没有多大关系:

  • You pick names that are meaningful and describe the purpose of the variable/function/other. 您选择有意义的名称并描述变量/函数/其他的目的。 (Key issue here is purpose; let the compiler deal with the type.) (这里的关键问题是目的;让编译器处理类型。)

  • Be consistent in how you apply any other aspects of the convention (indentation, casing, prefixes, use of underscores, etc). 在应用约定的任何其他方面(缩进,大小写,前缀,下划线的使用等)时,请保持一致。

Generally speaking, if code is well-written, the details of the convention don't matter much. 一般来说,如果代码编写得当,则约定的细节并不重要。

As for Hungarian and prefixes: Win32 and C++ still uses them somewhat, while .Net and C# don't. 至于匈牙利语和前缀:Win32和C ++仍会使用它们,而.Net和C#则不使用。

I'd highly recommend reading this long but very insightful article by Joel Spolsky that goes to great length to outline how and why a prefix convention can be actually very useful, if done correctly, and also where it's just a pointless chore. 我强烈建议您阅读Joel Spolsky撰写的这篇漫长而非常有见地的文章,其中详尽地概述了前缀约定如何以及为什么实际上可以非常有用(如果正确完成的话)以及在哪里毫无意义的琐事。

These days, I don't bother with most Hungarian prefixes, except for a few (note that these are just my personal preferences, they're ones that I've found to be useful over the years): 如今,除少数匈牙利前缀外,我不再打扰大多数匈牙利前缀(请注意,这些只是我的个人喜好,这些年来我发现它们是有用的):

  • p for pointers, because in C++, unlike C#, it's useful to know when you're dealing with a reference vs an object, and how many levels of indirection you're dealing with. p代表指针,因为与C#不同,在C ++中,知道何时要处理引用与对象以及知道要处理多少个间接级别是很有用的。
  • m_ for member variables (Sometimes _ in C# depending on existing code; see HostileFork's note below re _ as a prefix in C++.) m_表示成员变量(在C#中有时_,具体取决于现有代码;请参阅下面re_的HostileFork注释,作为C ++中的前缀。)
  • cch for character counts, cb for byte counts. cch用于字符计数,cb用于字节计数。 Its really important in Win32 to not get these mixed up; 在Win32中真正重要的是不要混淆这些内容。 pass a character count to memcpy or a byte count to GetWindowText, and you'll have trouble. 将字符计数传递给memcpy或将字节计数传递给GetWindowText,您将遇到麻烦。 This is the sort of use of prefixes that can help keep your code clear and obviously correct (or incorrect, as the case may be - "ah, of course, I'm passing a cch to memcpy, that's the problem!"). 这是使用前缀的一种方式,可以帮助您使代码清晰明了并且显然正确(或者视情况而定是不正确的-“啊,当然,我正在将cch传递给memcpy,这就是问题!”)。

For me, the first two of these help make the code more readable; 对我来说,前两个帮助使代码更易读。 the third example here helps both with readability, but can also be a useful technique for ensuring correctness - a bit like a mnemonic, if you will. 这里的第三个示例不仅有助于提高可读性,而且还是确保正确性的有用技术-如果可以的话,它有点像助记符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是Visual C ++ Win32还是MFC C ++? - Visual C++ Win32 or MFC C++? C ++,MFC,选项卡式文档界面应用程序,Win32 - C++, MFC, tabbed document interface application, Win32 How to export a function of a regular MFC DLL that is statically linked to MFC to use with a non MFC Win32 Console App in C++ - How to export a function of a regular MFC DLL that is statically linked to MFC to use with a non MFC Win32 Console App in C++ 将MFC gui添加到Win32 C ++命令行应用程序的好方法是什么? - What is a good way to add MFC gui to a Win32 C++ command line application? 在C ++ \\ Win32(不是MFC或CLI)中编写像按钮一样的控件? - Writing controls like a button in C++\Win32 (not MFC or CLI)? 如何将TIMESTAMP列从Oracle DB转换为C ++(MFC或WIN32)? - how to get TIMESTAMP column from Oracle DB into C++ (MFC or WIN32)? MFC/WIN32/C++:如何在进行计算密集型操作时分派消息? - MFC/WIN32/C++: How to dispatch messages while doing a computationally intensive operation? 如何使用 C++(win32/MFC) 跟踪准确的 windows 启动时间 - How to track the accurate windows startup time using C++(win32/MFC) 如何在Visual C ++ 2008中将MFC应用程序项目添加到Win32应用程序项目 - How to add MFC application project to Win32 application project in Visual C++ 2008 Win32消息泵与MFC消息映射,哪个更快? C ++ - Win32 Message Pump vs MFC Message Maps, which is faster? C++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM