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


推荐阅读
  • 线程漫谈——线程基础
    本系列意在记录Windwos线程的相关知识点,包括线程基础、线程调度、线程同步、TLS、线程池等。进程与线程理解线程是至关重要的,每个进程至少有一个线程,进程是线程的容器,线程才是真正的执行体,线程必 ... [详细]
  • vb.net不用多线程如何同时运行两个过程?不用多线程?即使用多线程,也不会是“同时”执行,题主只要略懂一些计算机编译原理就能明白了。不用多线程更不可能让两个过程同步执行了。不过可 ... [详细]
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • 本文介绍了lua语言中闭包的特性及其在模式匹配、日期处理、编译和模块化等方面的应用。lua中的闭包是严格遵循词法定界的第一类值,函数可以作为变量自由传递,也可以作为参数传递给其他函数。这些特性使得lua语言具有极大的灵活性,为程序开发带来了便利。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • Android源码深入理解JNI技术的概述和应用
    本文介绍了Android源码中的JNI技术,包括概述和应用。JNI是Java Native Interface的缩写,是一种技术,可以实现Java程序调用Native语言写的函数,以及Native程序调用Java层的函数。在Android平台上,JNI充当了连接Java世界和Native世界的桥梁。本文通过分析Android源码中的相关文件和位置,深入探讨了JNI技术在Android开发中的重要性和应用场景。 ... [详细]
  • Java学习笔记之面向对象编程(OOP)
    本文介绍了Java学习笔记中的面向对象编程(OOP)内容,包括OOP的三大特性(封装、继承、多态)和五大原则(单一职责原则、开放封闭原则、里式替换原则、依赖倒置原则)。通过学习OOP,可以提高代码复用性、拓展性和安全性。 ... [详细]
  • C++字符字符串处理及字符集编码方案
    本文介绍了C++中字符字符串处理的问题,并详细解释了字符集编码方案,包括UNICODE、Windows apps采用的UTF-16编码、ASCII、SBCS和DBCS编码方案。同时说明了ANSI C标准和Windows中的字符/字符串数据类型实现。文章还提到了在编译时需要定义UNICODE宏以支持unicode编码,否则将使用windows code page编译。最后,给出了相关的头文件和数据类型定义。 ... [详细]
  • 如何搭建Java开发环境并开发WinCE项目
    本文介绍了如何搭建Java开发环境并开发WinCE项目,包括搭建开发环境的步骤和获取SDK的几种方式。同时还解答了一些关于WinCE开发的常见问题。通过阅读本文,您将了解如何使用Java进行嵌入式开发,并能够顺利开发WinCE应用程序。 ... [详细]
  • Python脚本编写创建输出数据库并添加模型和场数据的方法
    本文介绍了使用Python脚本编写创建输出数据库并添加模型数据和场数据的方法。首先导入相应模块,然后创建输出数据库并添加材料属性、截面、部件实例、分析步和帧、节点和单元等对象。接着向输出数据库中添加场数据和历程数据,本例中只添加了节点位移。最后保存数据库文件并关闭文件。文章还提供了部分代码和Abaqus操作步骤。另外,作者还建立了关于Abaqus的学习交流群,欢迎加入并提问。 ... [详细]
  • 知识图谱——机器大脑中的知识库
    本文介绍了知识图谱在机器大脑中的应用,以及搜索引擎在知识图谱方面的发展。以谷歌知识图谱为例,说明了知识图谱的智能化特点。通过搜索引擎用户可以获取更加智能化的答案,如搜索关键词"Marie Curie",会得到居里夫人的详细信息以及与之相关的历史人物。知识图谱的出现引起了搜索引擎行业的变革,不仅美国的微软必应,中国的百度、搜狗等搜索引擎公司也纷纷推出了自己的知识图谱。 ... [详细]
  • 关键词:Golang, Cookie, 跟踪位置, net/http/cookiejar, package main, golang.org/x/net/publicsuffix, io/ioutil, log, net/http, net/http/cookiejar ... [详细]
  • 在重复造轮子的情况下用ProxyServlet反向代理来减少工作量
    像不少公司内部不同团队都会自己研发自己工具产品,当各个产品逐渐成熟,到达了一定的发展瓶颈,同时每个产品都有着自己的入口,用户 ... [详细]
  • 本文介绍了UVALive6575题目Odd and Even Zeroes的解法,使用了数位dp和找规律的方法。阶乘的定义和性质被介绍,并给出了一些例子。其中,部分阶乘的尾零个数为奇数,部分为偶数。 ... [详细]
  • 本文介绍了深入浅出Linux设备驱动编程的重要性,以及两种加载和删除Linux内核模块的方法。通过一个内核模块的例子,展示了模块的编译和加载过程,并讨论了模块对内核大小的控制。深入理解Linux设备驱动编程对于开发者来说非常重要。 ... [详细]
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社区 版权所有