Effective C++: 50 Specific Ways to Improve Your Programs and Design (2nd Edition) (Addison-Wesley Professional Computing)

Category: Programming
Author: Scott Meyers
All Stack Overflow 25
This Year Stack Overflow 2
This Month Stack Overflow 1


by casperOne   2019-07-21

You might want to pick up a copy of "Effective C++: 50 Specific Ways to Improve Your Programs and Design (2nd Edition)":


I've found it to be invaluable, and it's still very relevant today, even if you aren't programming in C++.

by anonymous   2019-07-21

Effective C++ by Scott Meyers

by anonymous   2019-07-21

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:

MyClass fun() {
    return MyClass(a, b, c);

MyClass x = fun();

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

It is also technically possible to store an object returned by value in a reference, like:

MyClass& x = fun();

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.

by anonymous   2018-03-19

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.

by anonymous   2018-03-19

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.

const                         /* this is a const object...     */
class nullptr_t
   template<class T>          /* convertible to any type       */
   operator T*() const        /* of null non-member            */
      { return 0; }           /* pointer...                    */

   template<class C, class T> /* or any type of null           */
      operator T C::*() const /* member pointer...             */
      { return 0; }   

   void operator&() const;    /* Can't take address of nullptr */

} nullptr = {};               /* and whose name is nullptr     */

That book is definitely worth reading.

The advantage of nullptr over NULL is, that nullptr acts like a real pointer type thus it adds type safety whereas NULL acts like an integer just set to 0 in pre C++11.

by anonymous   2017-08-20

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)

by anonymous   2017-08-20

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's unsigned and you don't generally want to access myarray[-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). Use new (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.

by anonymous   2017-08-20

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.

by anonymous   2017-08-20

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.

typedef time Time
typedef distance Distance
typedef velocity Velocity
typedef time float
typedef distance float
typedef velocity float

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:

class Time {
  float value;
  Time operator +(Time b) {self.value + b.value;}
  Time operator -(Time b) {self.value - b.value;}
  // don't define Time*Time, Time/Time etc.
  Time operator *(float b) {self.value * b;}
  Time operator /(float b) {self.value / b;}

class Distance {
  float value;
  Distance operator +(Distance b) {self.value + b.value;}
  // also -, but not * or /
  Velocity operator /(Time b) {Velocity( self.value / b.value )}

class Velocity {
  float value;
  // appropriate operators
  Velocity(float a) : value(a) {}

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.

by Tim   2017-08-20

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.

by Klaim   2017-08-20

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