[英]Resources to Write ANSI C++ Code
The last time I heavily used C++ was years ago, and it was strictly done on the Windows platform. 我上次大量使用C ++是在多年前,并且严格在Windows平台上完成。 Specifically, I used Microsoft Visual Studio as my IDE and developed some habitual patterns to use Microsoft's C++ version.
具体来说,我使用Microsoft Visual Studio作为我的IDE,并开发了一些惯用模式来使用Microsoft的C ++版本。 For example, I used void main() instead of the standard int main() .
例如,我使用void main()代替了标准的int main() 。
Now, I am taking a class where it is required to develop programs to be ANSI C++ compliant and the code will be compiled using g++. 现在,我上一堂课,要求开发符合ANSI C ++的程序,并且代码将使用g ++进行编译。 I quickly learned that system ( "PAUSE" ) does not work in g++ and is probably a Microsoft thing.
我很快了解到该系统(“ PAUSE”)在g ++中不起作用,可能是微软的事情。
Does anyone know of any good resources ( sites, tutorials, books ) where I can learn what more to be better ANSI C++ complaint? 有谁知道任何好的资源(网站,教程,书籍),在这里我可以学到更多更好的ANSI C ++投诉?
Thank you. 谢谢。
If you are using g++, then compile with the -pedantic and -std=c++98 flags. 如果使用的是g ++,请使用-pedantic和-std = c ++ 98标志进行编译。 The only standard for ANSI C++ is really the ISO Standard document, which cannot be recommended to a beginner.
ANSI C ++的唯一标准实际上是ISO标准文档,因此不建议初学者使用。 You are mistaken about system("PAUSE"), by the way - system() is part of ANSI C++ - it's parameter is not standardised, however.
顺便提一下,您对system(“ PAUSE”)感到误解-system()是ANSI C ++的一部分-但是其参数未标准化。
Most C++ books will be platform agnostic (of course unless they are made specifically for Visual C++). 大多数C ++书籍都是平台无关的(当然,除非它们是专门为Visual C ++编写的)。 Here are some good books that are recommended by the pros on ##C++
这是## C ++专业人士推荐的一些好书
There are plenty of good tutorials gcc, has a flag that will make sure the code is ANSI C compliant too, gcc -ansi -Wall -fsyntax-only -pedantic
gcc有很多不错的教程,有一个标志来确保代码也符合ANSI C,
gcc -ansi -Wall -fsyntax-only -pedantic
* Wall - turn on all errors
* ansi - use strict ANSI C specification
* fsyntax-only - only checks syntax
* pedantic - reject violations
Per, comments In addition you can use * -Wextra to turn on a few extra warnings, 注释,此外,您可以使用* -Wextra打开一些其他警告,
update thanks for update on capitalization., and mention of -pedantic 更新感谢大写的更新。并提及-pedantic
GCC will do a good job of telling you when your code is not ISO C++ compliant (not that it is an ISO standard, not an ANSI standard). 当您的代码不符合ISO C ++规范(不是它是ISO标准,不是ANSI标准)时,GCC会很好地告诉您。 Set the warning options -Werror -Wall, and simply fix all warnings;
设置警告选项-Werror -Wall,只需修复所有警告; you soon get out of non-compliant habits and ger fewer and fewer warnings.
您很快就会摆脱不合规的习惯,并发出越来越少的警告。
Lots of gcc fans. 很多gcc粉丝。
The important flags to compile with under Visual Studio to be as strict as possible are: 在Visual Studio下要严格编译的重要标志是:
cl /Za /W4 ...rest of command...
/Za disables Microsoft specific extensions and /W4 is the highest warning level (except /Wall, which complains about ridiculous things). / Za禁用Microsoft特定的扩展名,并且/ W4是最高警告级别(/ Wall除外,它抱怨可笑的事情)。
I'd also recommend you use a modern version of Visual Studio - old versions (VC6) were ridiculously non-conformant, and Microsoft officially pretends they never existed at this point. 我还建议您使用Visual Studio的现代版本-旧版本(VC6)非常不符合规范,Microsoft正式假装它们在此刻不存在。
I would highly recommend these two: 我强烈推荐这两个:
Granted, they both are not terribly great if you want a tutorial introduction to C++, but looks like you already know some C++, and need to learn more, and correct bad habits. 当然,如果您想要C ++的教程介绍,它们都不是非常棒,但是看起来您已经知道一些C ++,并且需要学习更多和纠正不良习惯。 From my personal experience, the above two are highly useful in doing exactly that.
从我的个人经验来看,以上两项对于完成此任务非常有用。
About comp.lang.c++
, make sure you fully read their FAQ and lurk there a while before posting. 关于
comp.lang.c++
,请确保您完全阅读了他们的FAQ并潜伏了一段时间再发布。 The same applies to stackoverflow of course, although lurking may not be necessary here. 当然,这同样适用于stackoverflow,尽管潜伏在这里不是必需的。
Using g++
, compile your programs with g++ -ansi -pedantic -Wall -Wextra -Weffc++
, and make sure you understand all the warnings. 使用
g++
,用g++ -ansi -pedantic -Wall -Wextra -Weffc++
编译程序,并确保您了解所有警告。 I use: 我用:
g++ -Wextra -Wall -Weffc++ -ansi -pedantic -Woverloaded-virtual \
-Wcast-align -Wpointer-arith
I would recommend. 我会推荐。
websites: 网站:
books: 图书:
talking / listening to experts and understanding what they have to say and why. 交谈/倾听专家的声音,并理解他们必须说的话以及原因。
C++ Primer (4th Ed) appears to be the best beginner book these days as it takes a modern approach and teaches all the important parts of the language. C ++ Primer(第四版)是当今最好的初学者书籍,因为它采用了现代化的方法并教授了语言的所有重要部分。 Accelerated C++ is another book often recommended by professionals and it serves as a good introduction, but I wouldn't recommend buying it anymore because C++ Primer nicely replaces it and covers the language better.
加速C ++是专业人士经常推荐的另一本书,它是不错的入门书,但是我不建议再购买它,因为C ++ Primer很好地替代了它,并且涵盖了更好的语言。
Note: C++ Primer Plus (which is a rather bad book) has nothing to do with C++ Primer. 注意:C ++ Primer Plus(这是一本相当糟糕的书)与C ++ Primer无关。
Stroustrup's The C++ Programming Language always needs to be mentioned, of course, because it is written by the father of the language. 当然,总是需要提及Stroustrup的C ++编程语言,因为它是由该语言的父亲编写的。 Many people find it enlightening, but I personally would not recommend it for learning the language.
许多人觉得它很有启发性,但是我个人不建议将它用于学习该语言。
All the books I mentioned only describe the ISO standard with no non-standard extensions. 我提到的所有书籍仅描述了ISO标准,没有非标准扩展名。 The programs in these books should work with any C++ compiler.
这些书中的程序应可与任何C ++编译器一起使用。
每当有疑问时,尤其是对标准C ++库,我都喜欢访问www.cplusplus.com。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.