Jump to content

C++


jhachem
 Share

Recommended Posts

http://cplusplus.com/and to be more precise - their tutorial, and their reference.Google is your friend btw. Searching for "*language* tutorial" usually gives good results. Edited by boen_robot
Link to comment
Share on other sites

http://cplusplus.com/ is not a good place to learn C++ (it's shallow and in some places inaccurate - or it advocates a C way over a C++ way), and anyone who's learned much will happily concur with me here.I would recommend you buy the book "Accelerated C++", but failing that google "Thinking in C++", that's an alright e-book, vastly more competent authors than cplusplus :) Edited by AElliott
Link to comment
Share on other sites

http://cplusplus.com/ is not a good place to learn C++ (it's shallow and in some places inaccurate - or it advocates a C way over a C++ way), and anyone who's learned much will happily concur with me here.I would recommend you buy the book "Accelerated C++", but failing that google "Thinking in C++", that's an alright e-book, vastly more competent authors than cplusplus :)
OK, maybe I haven't learned much about C++, so please, give an example... the only example where they advocate a C way over a C++ way I can think of is character sequences, but they do mention the string class at the start of the page. And when they talk about variables at the start of the tutorial, they use the string class too.
Link to comment
Share on other sites

I'm not going to give you a definitive list, but ok, here's one: http://cplusplus.com/doc/tutorial/program_structure/Line 4 of their first program has "using namespace std;" this is a bad idea for short programs, because the behaviour of that is not restricted to only bringing in the parts of the stdlib required for the program. It can bring in loads of unneeded symbols into the global scope. A book will instead introduce the more correct "using std::cout;", which is defined to only do what you need for that program. But since the tutorial there is quite shallow, it doesn't tell you that, it even suggests that it's a good idea - when it never is (and it's only an allowable idea if you're actually using a lot of the standard library).Another example of not really covering the C++ bits of it would be their section titled "Data Structures", which mentions as far as I can see none of the many provided by stdlib, and only mentions struct.Or there's their Polymorphism examples, which do not mention the fact that not defining virtual constructors/destructors etc will often lead to memory leak when handling derived classes from a base class pointer. This is something all polymorphic base classes should have, and is also a reason why you shouldn't try to have this behaviour using a stdlib class as a base class, because they do not have these virtual methods. Memory leak is never a good thing. :)The main complaint is about the shallow covering of concepts though, cases where they're actually wrong are not so common as the many cases where they don't explain things well enough.A few other cases where I feel more information should be provided: - constants are a bit more woolly than made out, so if you have a constant reference, the reference doesn't change but the data it refers to could. You can also mess around with volatility with const_cast<>s. The "value" therefore can be more mutable than you'd think. - the dangers of using goto are not discussed in "Control Structures", while there is a use for it, new programmers should definitely be warned that it can allow very messy code that jumps around in such a way that makes debugging very difficult. - arrays are discussed, but I don't see a mention of std::vector which seems somewhat obvious when you quite often want a dynamic length array, and C++98 does not have the support for this that C99 has (in the standard - compilers may hack it in)

Edited by AElliott
Link to comment
Share on other sites

I'm not going to give you a definitive list, but ok, here's one: http://cplusplus.com/doc/tutorial/program_structure/Line 4 of their first program has "using namespace std;" this is a bad idea for short programs, because the behaviour of that is not restricted to only bringing in the parts of the stdlib required for the program. It can bring in loads of unneeded symbols into the global scope. A book will instead introduce the more correct "using std::cout;", which is defined to only do what you need for that program. But since the tutorial there is quite shallow, it doesn't tell you that, it even suggests that it's a good idea - when it never is (and it's only an allowable idea if you're actually using a lot of the standard library).
Doesn't the compiler eliminate unused symbols in the final binary? I mean, sure... this will cause the program to compile more slowly, but AFAIK, it doesn't have any kind of effect on the final program. With that said, I don't see any serious downside to using namspace std.
Another example of not really covering the C++ bits of it would be their section titled "Data Structures", which mentions as far as I can see none of the many provided by stdlib, and only mentions struct.
It shows how to create data structures, and how to use them... in general. The reference shows the "Types" defined by each library. If you know how to use data structures in general, you know how to use the data structures in stdlib.
Or there's their Polymorphism examples, which do not mention the fact that not defining virtual constructors/destructors etc will often lead to memory leak when handling derived classes from a base class pointer. This is something all polymorphic base classes should have, and is also a reason why you shouldn't try to have this behaviour using a stdlib class as a base class, because they do not have these virtual methods. Memory leak is never a good thing. :)
You should probably contact cpluspus.com about that one, and give a simple concrete example of a memory leak and how to avoid it. If you won't contact them, I would... just give the example here.
The main complaint is about the shallow covering of concepts though, cases where they're actually wrong are not so common as the many cases where they don't explain things well enough.
Normal for any kind of "tutorial" I'd say... the idea of tutorials is to jump start you, not to get into implmementaion details.
- constants are a bit more woolly than made out, so if you have a constant reference, the reference doesn't change but the data it refers to could. You can also mess around with volatility with const_cast<>s. The "value" therefore can be more mutable than you'd think.
Volatility?
- the dangers of using goto are not discussed in "Control Structures", while there is a use for it, new programmers should definitely be warned that it can allow very messy code that jumps around in such a way that makes debugging very difficult.
They do say "You should use this feature with caution"... tutorials shouldn't take any definitive side about being "for" or "against" a certain feature. They should only warn about it (which they do), but still demonstrate its use.
- arrays are discussed, but I don't see a mention of std::vector which seems somewhat obvious when you quite often want a dynamic length array, and C++98 does not have the support for this that C99 has (in the standard - compilers may hack it in)
OK. I agree this is a miss. But then again, they don't discuss many of the features in the standard library. They only show input/output with files. You still have the reference though. Edited by boen_robot
Link to comment
Share on other sites

The C++98 standard says nothing about compilers having to screen binaries for unwanted symbols. If you're relying on undefined behaviour you're using C++ wrongly most likely. It's possible a particular compiler or two will be strict about this, but since it's not in the standard a compiler that doesn't do this isn't doing anything wrong. So... you can make excuses, but it's still wrong.I completely disagree that the "Data Structures" section is remotely sufficient. Knowing how a struct{} works (and to be honest, it doesn't even go into implementation details here when it's very easy to do so - they could've mentioned that C++ structs are classes where everything defaults to public access) does not give you any of the information you really need to evaluate whether you should be using std::map or std::set, or std::list. The simple fact it doesn't even mention a load of these (when that's standard C++) is a pretty solid example of ignoring C++ solutions for inferior (generally, you can implement something that matches C++, but newbies can't) C-style solutions.I gave an example, handling a derived class from a base class pointer when the base class doesn't have a virtual dtor, you'll get memory leak because the destructor doesn't free up the memory reserved for the class properly. If you want a reference for this in print, then mention "Effective C++ - Third Edition" by Scott Meyers. Item 7, page 40.Because it's normal doesn't mean it's good. Shallow is generally always bad in the long run.Something that's volatile is changeable, and I was describing cases where if you have say a const reference, the data pointed to can change despite you handling it from a const. That sort of thing could be confusing in practice if you don't expect it.They warn, but give no reason. As a newbie what are you meant to be cautious about? I don't think they will know, and if they don't understand why to avoid it, who's to say they will?The very fact they like to ignore standard library features is why it's a sucky way to learn standard C++. I just don't see cplusplus.com as any form of replacement for a decent book that will go into more detail, explain things more completely and give the person learning a better overall understanding of the language and the concepts behind it. It seems kinda obvious to me that the (good) books are the better option.

Edited by AElliott
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...