Effective C++: 50 Specific Ways to Improve Your Programs and Design (2nd Edition) (Addison-Wesley Professional Computing)
All
Stack Overflow 25
This Year
Stack Overflow 2
This Month
Stack Overflow 1
You might want to pick up a copy of "Effective C++: 50 Specific Ways to Improve Your Programs and Design (2nd Edition)":
http://www.amazon.com/Effective-Specific-Addison-Wesley-Professional-Computing/dp/0201924889
I've found it to be invaluable, and it's still very relevant today, even if you aren't programming in C++.
Effective C++ by Scott Meyers
Recommended reading: Effective C++ by Scott Meyers. You find a very good explanation about this topic (and a lot more) in there.
In brief, if you return by value, the copy constructor and the destructor will be involved by default (unless the compiler optimizes them away - that's what happens in some of your cases).
If you return by reference (or pointer) a variable which is local (constructed on the stack), you invite trouble because the object is destructed upon return, so you have a dangling reference as a result.
The canonical way to construct an object in a function and return it is by value, like:
If you use this, you don't need to worry about ownership issues, dangling references etc. And the compiler will most likely optimize out the extra copy constructor / destructor calls for you, so you don't need to worry about performance either.
It is possible to return by reference an object constructed by
new
(i.e. on the heap) - this object will not be destroyed upon returning from the function. However, you have to destroy it explicitly somewhere later by callingdelete
.It is also technically possible to store an object returned by value in a reference, like:
However, AFAIK there is not much point in doing this. Especially because one can easily pass on this reference to other parts of the program which are outside of the current scope; however, the object referenced by
x
is a local object which will be destroyed as soon as you leave the current scope. So this style can lead to nasty bugs.K&R and Stroustrup are classics, and eventually you should get them, but I don't think they are good introduction for C++ beginners. Thinking in modern C++ is thinking in classes, templates, exceptions, and streams, none of which available in C language.
I would recommend a college-level textbook on C++ like Deitel and Deitel. alt text http://ecx.images-amazon.com/images/I/61dECNkdnTL._SL500_AA240_.jpg
After playing around, you should focus on learning to write a class that behaves like a built-in class. That means providing a copy constructor,
operator=
,operator==
,operator<<
, etc.. Along the way you'll meet various concepts embedded in the language of C++. I would agree with others on Effective C++ is a must read once you are comfortable with the basics.How to do that is mentioned in the book: Effective C++, 2nd edition by Scott Meyers (newer edition is available) in chapter: "Item 25: Avoid overloading on a pointer and a numerical type.".
It is needed if your compiler doesn't know the
nullptr
keyword introduced by C++11.That book is definitely worth reading.
The advantage of
nullptr
overNULL
is, thatnullptr
acts like a real pointer type thus it adds type safety whereasNULL
acts like an integer just set to 0 in pre C++11.You might wnat to check out The Definitive C++ Book Guide and List
For your purposes I would especially recommend:
They are not in particular order, also you might want to read and code something in between them.
(Note: As noted by @John Dibling the Boost book might be a bit out of date, I do not have experience with that one myself)
In general, I use whatever minimizes the number of implicit or explicit casts and warning errors. Generally there is a good reason why things are typed the way they are.
size_t
is a good choice for array index, since it'sunsigned
and you don't generally want to accessmyarray[-1]
, say.btw since this is C++ you should get out of the habit of using
malloc
(free
) which is part of CRT (C runtime library). Usenew
(delete
), preferably with smart pointers to minimize manual memory handling.Once you have mastered the basics, a good practices reference (language-specific) is Effective C++ by Scott Meyers. The logical next step is Effective STL.
If you are looking to continue with gaming, C++ is a darn good place to start. You could also check out C# as it is used by Microsoft in XNA (XBox), Second Life, and by Unity on smart phones.
While I would not disagree with anybody about the math and reading you need to do, I think it is better to just roll up your sleeves and program. Read other people's code and then read so that you can understand why they are doing what they are doing.
If you think you know C++ well, the next step is to learn how to use it to make better software. For that, I would start with reading Effective C++.
If you really have a handle on C++ then perhaps this StackOverflow thread will answer your question about game books.
Personally, even if your goal is game programming, I would branch out from that to get a solid grounding as a developer.
Perhaps you should do a little web programming to get a feel for what that is all about. Maybe something like Ruby on Rails.
Alternatively, you could try to write a simple compiler or even an operating system to get a feel for what goes on under the hood and to learn that these too are just programs written by mortals.
Instead of writing your own, you could also get involved in an Open Source project. If I had the time, I would be all over spending time reading the code of Haiku and finding somewhere to contribute. Here is a list of open source game projects that you could consider joining as well.
Your chances of getting a decent job probably go up quite a bit if you know Java or .NET so those are also options. If you decide on .NET, I recommend checking out Mono.
strong typing of ints (and floats etc) in c++
Scott Meyer (Effective c++ has a very effective and powerful solution to your problem of doing strong typing of base types in c++, and it works like this:
Strong typing is a problem that can be addressed and evaluated at compile time, which means you can use the ordinals (weak typing) for multiple types at run-time in deployed apps, and use a special compile phase to iron out inappropriate combinations of types at compile time.
You then define your
Time
,Mass
,Distance
to be classes with all (and only) the appropriate operators overloaded to the appropriate operations. In pseudo-code:Once this is done, your compiler will tell you any places you have violated the rules encoded in the above classes.
I'll let you work out the rest of the details yourself, or buy the book.
Effective C++ and More Effective C++
Other than that, pick a (small?) personal project you want to write and do it in C++. You are not going to learn C++ by reading a 1000 line project.
I think you'd better have some lectures about good practices and why they are good. That should help you more than a code analysis tool (in the beginning at least).
I suggest you read the series of Effective C++ and **Effective STL books, at least. See alsot The Definitive C++ Book Guide and List