热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

SimpleTipsonC++(对于C++的一些建议)

Introduction(简介)Forbeingapowerfulobject-orientedprogramminglanguage,Cisuseda

Introduction(简介)
For being a powerful object-oriented programming language, C++ is used almost everywhere including graphics and games section.
(C++作为一个强有力的面向对象语言,应用于包括图形、游戏等所有领域中。)

Complex features like multiple inheritance, template programming has made the language more powerful.
(向多重继承、泛型编程等特征使这门语言更加的强有力。)
So if you want to create powerful computer software or games, you should learn C++ properly and effectively.
(如果你想创造强有力的电脑软件和游戏,你应该正确高效的学习C++。)

To the novice programmer, C++ is always more complex and hard programming language than it actually is. I am saying this from my own experience. It is true that C++ is a complex featured programming language. But do not be afraid, once you have realized the importance of all the features of this language, you will feel the pleasure of learning this language.
(对于新手程序员,C++总是更加复杂和艰苦的编程语言,它实际就是这样。我从我自己的经验说这句话的。的确,C ++是一个复杂的功能的编程语言。但是不要害怕,一旦你已经意识到了这种语言的所有功能的重要性,你会觉得学习这门语言的乐趣。)

What IDE Should You Use to Write a C++ Program?
Well, if you are using Windows as your platform and looking for a C++ IDE, then I suggest the Microsoft Visual Studio. It has a fully-featured powerful C++ programming environment. Its various features such as the IntelliSense will help you to write programs more quickly and correctly. Its debugging system will let you correct your program. You will feel comfortable writing C++ program using this IDE.
(你应该使用什么样的IDE来写C ++程序?
这样,如果你使用的是Windows为平台,寻找一个C ++ IDE,那么我建议微软的Visual Studio。它有一个全功能强大的C++编程环境。它的各种功能,如智能感知将帮助您更快速,准确地编写程序。其调试系统将让你纠正你的程序。使用这个IDE编写C ++程序你会感到很舒服。)

How Can You Write a Good C++ Program?
A good program must be sufficiently easy to read, modify and extend. So your program should be easier to read by at least by yourself and there should not be any confusion when reading the program.
(你怎么能写一个好的C ++程序?
一个好的程序必须有足够的易阅性,可修改性和扩展性。所以,至少对于你自己来说,你的程序应该是更易于阅读,在读取程序时,不应该有任何混淆。)
If you are not be able to read your program easily, then how can you know whether yours program is going to work or not?
(如果你不能轻松的阅读自己的程序,你如何知道你的程序是否能正常工作呢?)

If any portion of your code is looking confusing or disturbing, your mind then deletes it and writes it again properly.
(如果你代码的任何部分看上去使人迷惑和令人不安,你的想法应该是立刻删除它,并且重新编写。)

C++ is a case-sensitive programming language. So always be careful about that.
(C ++是一种区分大小写的编程语言。所以,一定要加倍小心这一点。)
The following things will help you to improve readability of your program.
(接下来的事情将会帮助你提高你的程序的可阅读性。)

Variable Naming
(变量命名)

Use a deserving name for each variable of your program so that it will be easier for you to understand what purpose that variable was made for.
(为您的程序的每个变量使用一个值得的名称,这样它会更容易让你了解这个变量是为什么目的。)

Horrible variable naming can be also a great cause of program bug, at least for a novice programmer.
(至少对于一个新手程序员来说,恐怖的变量命名会引起程序很大的bug。)

int Money = 100;
const char* Name = "Shuvo";
printf("%s has only %d taka!\n", Name, Money);

Instead of:
(替换)

int Var1 = 100;
const char* Var2 = "Shuvo";
printf("%s has only %d taka!\n", Var2, Var1);
Use 'g_' or 'g' prefix for global variable and use 'm_' or 'm' prefix for member variable.

Use different prefix for each data-type such as ‘n’ or ‘i’ for integer variables, ‘f’ for floating type variables, ‘u’ for unsigned type, ‘p’ for any kind of pointers.
Use ‘str’ prefix for string type ( std::string ) variables.
(对于不同的变量类型,使用不同的前缀。比如说对于整形变量使用’n’或’i’,对于浮点型使用’f’,对于无符号类型使用’u’,对于各种类型的指针可以使用’p’。)

Use of data-type prefix will help you to figure out variables data-type, especially when your Code Editor does not support IntelliSense like feature.
(所有的数据类型的加前缀都会使你分清变量类型,尤其是你的编译器不支持智能推断。)

I am not saying that you must have to use those prefix and of course you can use your own variable name prefix that will help you much more than those prefixes.
(我不是说你必须使用这些前缀进行命名,你可以使用你自己的方式进行前缀命名。)

Function Naming
(函数命名)

Use a suitable name for each function of your program so that it will be easier for you to understand what intention the function was made for and what kind of job is doing the function.
(为您的程序的每个函数使用一个合适的名称,这样它会更容易让你明白为什么写这个函数,这个函数所做的工作是什么样子的。)

Use a different naming style for global and member functions that will help you to remember when you invoke the function. Although you can use the ‘::’ operator of C++ language for that purpose.
(对于全局函数和成员函数,使用不同的命名方式会帮助你记住合适调用这个函数。尽管在C++中你可以使用操作符::来显现类成员函数。)

Use of Comment
(使用注释)
Use comments at any place of your program where there is a possibility of confusion. It will help you remember which statement of your program is written for what purpose. It also helps when you later try to update your source code.
(在你的程序容易引起困惑的地方需要进行注释。这将会帮你记住你写此程序的意图。同样也会帮你记住你最后一次修改这段代码是什么时候。)

In C++, ‘// …..’ is used for single-line comment and ‘/* ….. */’ is used for multi-line comment.
(在C++中’// …..’ 用于单行注释,’/* ….. */’用于多行注释)

If you are working with several projects, I strongly suggest that you use enough comments inside each project source code.
(如果你的工作有几个工程,我强烈建议你在源代码中加入足够的注释。)

Keeping the Memory Free
(不要内存泄露)
This is the most painful task for a C++ programmer. Novice C++ programmer often forgets to deallocate the allocated memory and this makes a program buggy and memory cost.
(这是一个C ++程序员最痛苦的任务。初学C++程序员经常忘记释放分配的内存,这使得程序缺陷和存储成本。)

So do not let your program crash the user computer as well as your computer. Think more about memory management of your program.
(所以,不要让你的程序崩溃用户电脑,以及您的计算机。多想想你的程序的内存管理。)

When freeing a pointer, always check whether that pointer actually exists so that reduces program crash. You can do it in this way:
(当释放指针,请务必检查指针是否实际存在,这样减少了程序崩溃。你能做到这样:)

if ( ptr != NULL )
{delete ptr;ptr = NULL;
}

Instead of:
(替换)

delete ptr;

Organize Your Project Source Code
(组织你的工程源代码)
If you are going to develop a big project that will need over thousand lines of source code, then do not gather all your source code into a single CPP source file. It will force your C++ compiler to compile the whole source-code even for every little modification.
(如果你要开发将需要超过千行源代码的大项目,那么就不要收集所有的源代码到一个单一的CPP源文件。它会迫使你的C ++编译器编译整个源代码,甚至对每个小的修改。)

Divide your source code into several CPP source and header files. This will let your compiler compile only those source files that you have modified. So it will save your time.
(把你的源代码为几个CPP源文件和头文件。这将让你的编译器编译只已修改的源文件。因此,这将节省您的时间。)


推荐阅读
  • 从 .NET 转 Java 的自学之路:IO 流基础篇
    本文详细介绍了 Java 中的 IO 流,包括字节流和字符流的基本概念及其操作方式。探讨了如何处理不同类型的文件数据,并结合编码机制确保字符数据的正确读写。同时,文中还涵盖了装饰设计模式的应用,以及多种常见的 IO 操作实例。 ... [详细]
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • 本文深入探讨了 Java 中的 Serializable 接口,解释了其实现机制、用途及注意事项,帮助开发者更好地理解和使用序列化功能。 ... [详细]
  • 本文详细介绍了Akka中的BackoffSupervisor机制,探讨其在处理持久化失败和Actor重启时的应用。通过具体示例,展示了如何配置和使用BackoffSupervisor以实现更细粒度的异常处理。 ... [详细]
  • 本文详细介绍了Java编程语言中的核心概念和常见面试问题,包括集合类、数据结构、线程处理、Java虚拟机(JVM)、HTTP协议以及Git操作等方面的内容。通过深入分析每个主题,帮助读者更好地理解Java的关键特性和最佳实践。 ... [详细]
  • DNN Community 和 Professional 版本的主要差异
    本文详细解析了 DotNetNuke (DNN) 的两种主要版本:Community 和 Professional。通过对比两者的功能和附加组件,帮助用户选择最适合其需求的版本。 ... [详细]
  • 本文介绍如何使用阿里云的fastjson库解析包含时间戳、IP地址和参数等信息的JSON格式文本,并进行数据处理和保存。 ... [详细]
  • PyCharm下载与安装指南
    本文详细介绍如何从官方渠道下载并安装PyCharm集成开发环境(IDE),涵盖Windows、macOS和Linux系统,同时提供详细的安装步骤及配置建议。 ... [详细]
  • Explore a common issue encountered when implementing an OAuth 1.0a API, specifically the inability to encode null objects and how to resolve it. ... [详细]
  • 本文详细介绍如何使用Python进行配置文件的读写操作,涵盖常见的配置文件格式(如INI、JSON、TOML和YAML),并提供具体的代码示例。 ... [详细]
  • Java 中的 BigDecimal pow()方法,示例 ... [详细]
  • c# – UWP:BrightnessOverride StartOverride逻辑 ... [详细]
  • Scala 实现 UTF-8 编码属性文件读取与克隆
    本文介绍如何使用 Scala 以 UTF-8 编码方式读取属性文件,并实现属性文件的克隆功能。通过这种方式,可以确保配置文件在多线程环境下的一致性和高效性。 ... [详细]
  • 解决JAX-WS动态客户端工厂弃用问题并迁移到XFire
    在处理Java项目中的JAR包冲突时,我们遇到了JaxWsDynamicClientFactory被弃用的问题,并成功将其迁移到org.codehaus.xfire.client。本文详细介绍了这一过程及解决方案。 ... [详细]
  • 使用GDI的一些AIP函数我们可以轻易的绘制出简 ... [详细]
author-avatar
手机用户2502919831
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有