Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
added 616 characters in body
Source Link
Loki Astari
  • 97.7k
  • 5
  • 126
  • 341

For such a simple class I don't think you need to worry about the safe bool idium. This just protects your class from being used in places where arithmatic would be done:

 Test t;
 int x = 5 + t; // Here we get conversion to bool
 // which is then converted to int (0 or 1)
 // The safe bool idium would protect you from this:

To use it do this:

 operator bool() { return j%26; }
 // Remove the above and replace with
 operator void*() {return j%26 ? &j /* or this the value is not important as long as it is not NULL */ : NULL; } 

For such a simple class I don't think you need to worry about the safe bool idium. This just protects your class from being used in places where arithmatic would be done:

 Test t;
 int x = 5 + t; // Here we get conversion to bool
 // which is then converted to int (0 or 1)
 // The safe bool idium would protect you from this:

To use it do this:

 operator bool() { return j%26; }
 // Remove the above and replace with
 operator void*() {return j%26 ? &j /* or this the value is not important as long as it is not NULL */ : NULL; } 
Source Link
Loki Astari
  • 97.7k
  • 5
  • 126
  • 341

For simple 10 lines 10 programs you can get away with this:

using namespace std;

Anything bigger and it starts to be a pest more than a help. Thus it best not to get into the habit of using it. Anyway it is simpler even in this case just to prefex cout with std:: (that is why standard is named std to make it short and easy to use).

You don't initialize the value of j in your class.

int j;

Thus any usage is undefined behavior.

You have two options:

  1. Add a constructor that initaizes j
  2. Always force zero-initialization of your class.

In anything but this trivial program I would go with (1) and add a constructor. But just to show zero initialization the alternative is:

 Test t = Test(); // The () brackets at the end of Test() force zero initialization;

Note: The equivalent for new is

 Test* t = new Test(); // force zero-initialization (when no constructor)
 Test* t = new Test; // Use default-initialization (un-initialized)

Note 2: Normally you would think you could do this:

 Test t(); // Unfortunately this is a forward declaration of a function.

Note 3: But you can get around the problems of (2) by using extra braces. As pointed out last time I tried to answer a question like this.

 Test t(()); // Unfortunately this confuses people and maintainers have been
 // known to try and take out the extra braces. With mixed results.
 // This is why I still prefer method 1
 Test t = Test(); // This may look like there is an extra copy.
 // But every compiler implements the optimization to do construction
 // in place.
 // Thus I prefer method 1 (ie this method)

Described here: http://stackoverflow.com/questions/5914422/proper-way-to-initialize-c-structs/5914697#5914697

lang-cpp

AltStyle によって変換されたページ (->オリジナル) /