views:

313

answers:

9

The last time I heavily used C++ was years ago, and it was strictly done on the Windows platform. Specifically, I used Microsoft Visual Studio as my IDE and developed some habitual patterns to use Microsoft's C++ version. For example, I used void main() instead of the standard 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++. I quickly learned that system ( "PAUSE" ) does not work in g++ and is probably a Microsoft thing.

Does anyone know of any good resources ( sites, tutorials, books ) where I can learn what more to be better ANSI C++ complaint?

Thank you.

+2  A: 

If you are using g++, then compile with the -pedantic and -std=c++98 flags. The only standard for ANSI C++ is really the ISO Standard document, which cannot be recommended to a beginner. You are mistaken about system("PAUSE"), by the way - system() is part of ANSI C++ - it's parameter is not standardised, however.

anon
+1  A: 

Most C++ books will be platform agnostic (of course unless they are made specifically for Visual C++). Here are some good books that are recommended by the pros on ##C++

joshperry
This is a great list. Thank you.
different
+1  A: 

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

* 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,

update thanks for update on capitalization., and mention of -pedantic

Evan Carroll
That's -Wall I think.
anon
you've got the capitalization wrong, it's actually `-Wall`. also, the gcc/g++ manpage says *In C++ mode, remove GNU extensions that conflict with ISO C++. [...] The `-ansi` option does not cause non‐ISO programs to be rejected gratuitously. For that, `-pedantic` is required in addition to -ansi.*
just somebody
manpage says -> manpage says about `-ansi`
just somebody
don't forget -Wextra
swegi
A: 

I would highly recommend these two:

  • comp.lang.c++ Usenet newsgroup. If you can get hold of a good Usenet service provider, and use a news reader, you should be able to get rid of the spam. I use eternal-september.org, and like it a lot.
  • Read the C++ FAQ. It has a lot of great information.

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. 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. The same applies to stackoverflow of course, although lurking may not be necessary here.

Using g++, compile your programs with g++ -ansi -pedantic -Wall -Wextra -Weffc++, and make sure you understand all the warnings. I use:

g++ -Wextra -Wall -Weffc++ -ansi -pedantic -Woverloaded-virtual \
-Wcast-align -Wpointer-arith
Alok
I would highly recommend avoiding comp.lang.c++ (and I'm still one of it's all-time top 10 posters) - you are much better off asking C++ questions here. If you want to use Usenet, go to http://groups.google.com/group/comp.lang.c++.moderated/topics
anon
@Neil: Why avoid `comp.lang.c++`?
Alok
Spam (which you can avoid to a certain extent using a newsreader, as you said), trolls (which you can't) and some not very bright people. The moderated group is much better.
anon
Yes. The same is true about `comp.lang.c` for example. But after reading the newsgroup for a while, you learn whose advice to ignore, and who know what they're saying. `comp.lang.c++` has more interesting discussion, at the expense of needing more weeding.
Alok
+1  A: 

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). Set the warning options -Werror -Wall, and simply fix all warnings; you soon get out of non-compliant habits and ger fewer and fewer warnings.

Clifford
+1  A: 

Lots of gcc fans.

The important flags to compile with under Visual Studio to be as strict as possible are:

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).

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.

Terry Mahaffey
I currently using Microsoft Visual Studio 2008, and based on all these comments installed gcc as well. I have never heard of this compiler before.
different
A: 

I would recommend.

websites:

books:

  • effective C++
  • effective stl
  • modern c++ design
  • template metaprogramming

talking / listening to experts and understanding what they have to say and why.

Rick
A: 

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. 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.

Note: C++ Primer Plus (which is a rather bad book) has nothing to do with 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. 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. The programs in these books should work with any C++ compiler.

Tronic
anon
The authors of the 4th edition are Lippman, Lajoie and Moo, and the new edition is very different from the 3rd edition. While AC++ is a good introduction, with a modern approach, it only covers a very small portion of the language. C++ Primer also takes that same modern approach (thanks to Moo, I must assume). Since C++ Primer is also far more complete, I see no reason in buying AC++ anymore. The answer edited (after your comment) to clarify what I meant.
Tronic
This review at Amazon clarifies the issue: http://www.amazon.com/review/RLTC8B1UR0LRQ/ref=cm_cr_pr_viewpnt#RLTC8B1UR0LRQAndrew Koenig, the other author of AC++, also comments on this review: http://www.amazon.com/review/R2V99WR8ZNX4NM/ref=cm_cr_pr_viewpnt#R2V99WR8ZNX4NM(the two reviews linked are the highest approved positive and negative reviews of the book)
Tronic
A: 

I like visiting www.cplusplus.com whenever I have a doubt, specially about the standard C++ library.

f4