热门标签 | 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源文件和头文件。这将让你的编译器编译只已修改的源文件。因此,这将节省您的时间。)


推荐阅读
  • 拥抱Android Design Support Library新变化(导航视图、悬浮ActionBar)
    转载请注明明桑AndroidAndroid5.0Loollipop作为Android最重要的版本之一,为我们带来了全新的界面风格和设计语言。看起来很受欢迎࿰ ... [详细]
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • Android源码深入理解JNI技术的概述和应用
    本文介绍了Android源码中的JNI技术,包括概述和应用。JNI是Java Native Interface的缩写,是一种技术,可以实现Java程序调用Native语言写的函数,以及Native程序调用Java层的函数。在Android平台上,JNI充当了连接Java世界和Native世界的桥梁。本文通过分析Android源码中的相关文件和位置,深入探讨了JNI技术在Android开发中的重要性和应用场景。 ... [详细]
  • 如何搭建Java开发环境并开发WinCE项目
    本文介绍了如何搭建Java开发环境并开发WinCE项目,包括搭建开发环境的步骤和获取SDK的几种方式。同时还解答了一些关于WinCE开发的常见问题。通过阅读本文,您将了解如何使用Java进行嵌入式开发,并能够顺利开发WinCE应用程序。 ... [详细]
  • 预备知识可参考我整理的博客Windows编程之线程:https:www.cnblogs.comZhuSenlinp16662075.htmlWindows编程之线程同步:https ... [详细]
  • 本文介绍了在Mac上安装Xamarin并使用Windows上的VS开发iOS app的方法,包括所需的安装环境和软件,以及使用Xamarin.iOS进行开发的步骤。通过这种方法,即使没有Mac或者安装苹果系统,程序员们也能轻松开发iOS app。 ... [详细]
  • Java和JavaScript是什么关系?java跟javaScript都是编程语言,只是java跟javaScript没有什么太大关系,一个是脚本语言(前端语言),一个是面向对象 ... [详细]
  • 本文介绍了Java集合库的使用方法,包括如何方便地重复使用集合以及下溯造型的应用。通过使用集合库,可以方便地取用各种集合,并将其插入到自己的程序中。为了使集合能够重复使用,Java提供了一种通用类型,即Object类型。通过添加指向集合的对象句柄,可以实现对集合的重复使用。然而,由于集合只能容纳Object类型,当向集合中添加对象句柄时,会丢失其身份或标识信息。为了恢复其本来面貌,可以使用下溯造型。本文还介绍了Java 1.2集合库的特点和优势。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • 如何用UE4制作2D游戏文档——计算篇
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了如何用UE4制作2D游戏文档——计算篇相关的知识,希望对你有一定的参考价值。 ... [详细]
  • 从零学Java(10)之方法详解,喷打野你真的没我6!
    本文介绍了从零学Java系列中的第10篇文章,详解了Java中的方法。同时讨论了打野过程中喷打野的影响,以及金色打野刀对经济的增加和线上队友经济的影响。指出喷打野会导致线上经济的消减和影响队伍的团结。 ... [详细]
  • 本文介绍了在Linux下安装和配置Kafka的方法,包括安装JDK、下载和解压Kafka、配置Kafka的参数,以及配置Kafka的日志目录、服务器IP和日志存放路径等。同时还提供了单机配置部署的方法和zookeeper地址和端口的配置。通过实操成功的案例,帮助读者快速完成Kafka的安装和配置。 ... [详细]
  • 本文介绍了最长上升子序列问题的一个变种解法,通过记录拐点的位置,将问题拆分为左右两个LIS问题。详细讲解了算法的实现过程,并给出了相应的代码。 ... [详细]
  • 本文介绍了如何使用Express App提供静态文件,同时提到了一些不需要使用的文件,如package.json和/.ssh/known_hosts,并解释了为什么app.get('*')无法捕获所有请求以及为什么app.use(express.static(__dirname))可能会提供不需要的文件。 ... [详细]
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社区 版权所有