Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
  • You don't need return 0 at the end of main(). Reaching that point already implies a successful termination, so the compiler will insert it for you.

  • Prefer to avoid raw pointers and manual memory management whenever possible. Instead, utilize standard containers and C++11 smart pointers.

    With raw pointers as data members, you would have to maintain The Rule of Three The Rule of Three (or The Rule of Five The Rule of Five in C++11) because the provided copy constructor and assignment operator will only copy the pointers (shallow copy), not the data they point to (deep copy).

  • Utilize initializer lists for your classes:

    Ball:

     Ball::Ball(int x, int y)
     : status(READY)
     , x(x)
     , y(y)
     , bounce(false);
     , speed(8);
     , angle(0.0f);
     , hits(0); 
     , predicted_y(0)
     {}
    

    Paddle:

     Paddle::Paddle(int new_x, int new_y)
     : x(new_x)
     , y(new_y)
     {}
    

    This will especially be helpful in case you ever need to initialize const members.

  • Paddle's accessors should be const as they do not modify data members:

     int Paddle::get_x() const {
     return x;
     }
    
     int Paddle::get_y() const {
     return y;
     }
    
  • Your randomization initializations should be put into an anonymous namespace:

     namespace {
     std::random_device rd;
     std::mt19937 gen(rd());
     }
    

    This will prevent linker errors in case the same names are used in other files.

  • This:

     int sign;
     if (paddle->get_x() < Pong::SCREEN_WIDTH/2)
     sign = 1;
     else
     sign = -1;
    

    can become a single-line ternary statement:

     int sign = (paddle->get_x() < Pong::SCREEN_WIDTH/2) ? 1 : -1;
    
  • You're using sin() and cos(), but you haven't included <cmath>. I assume your compiler is being lenient for some reason and is not raising errors. You should still include this, as well as prefixing the functions with std::.

  • In collides_with(), I'd condense all the if/else if statements into one if, with each condition separated by a ||. Only one of them has to be met to return false.

    You'll then have something in this form (separate || lines may be necessary):

     if (condition1 || condition2 || condition3)
     return false;
     else
     return true;
    
  • You don't need return 0 at the end of main(). Reaching that point already implies a successful termination, so the compiler will insert it for you.

  • Prefer to avoid raw pointers and manual memory management whenever possible. Instead, utilize standard containers and C++11 smart pointers.

    With raw pointers as data members, you would have to maintain The Rule of Three (or The Rule of Five in C++11) because the provided copy constructor and assignment operator will only copy the pointers (shallow copy), not the data they point to (deep copy).

  • Utilize initializer lists for your classes:

    Ball:

     Ball::Ball(int x, int y)
     : status(READY)
     , x(x)
     , y(y)
     , bounce(false);
     , speed(8);
     , angle(0.0f);
     , hits(0); 
     , predicted_y(0)
     {}
    

    Paddle:

     Paddle::Paddle(int new_x, int new_y)
     : x(new_x)
     , y(new_y)
     {}
    

    This will especially be helpful in case you ever need to initialize const members.

  • Paddle's accessors should be const as they do not modify data members:

     int Paddle::get_x() const {
     return x;
     }
    
     int Paddle::get_y() const {
     return y;
     }
    
  • Your randomization initializations should be put into an anonymous namespace:

     namespace {
     std::random_device rd;
     std::mt19937 gen(rd());
     }
    

    This will prevent linker errors in case the same names are used in other files.

  • This:

     int sign;
     if (paddle->get_x() < Pong::SCREEN_WIDTH/2)
     sign = 1;
     else
     sign = -1;
    

    can become a single-line ternary statement:

     int sign = (paddle->get_x() < Pong::SCREEN_WIDTH/2) ? 1 : -1;
    
  • You're using sin() and cos(), but you haven't included <cmath>. I assume your compiler is being lenient for some reason and is not raising errors. You should still include this, as well as prefixing the functions with std::.

  • In collides_with(), I'd condense all the if/else if statements into one if, with each condition separated by a ||. Only one of them has to be met to return false.

    You'll then have something in this form (separate || lines may be necessary):

     if (condition1 || condition2 || condition3)
     return false;
     else
     return true;
    
  • You don't need return 0 at the end of main(). Reaching that point already implies a successful termination, so the compiler will insert it for you.

  • Prefer to avoid raw pointers and manual memory management whenever possible. Instead, utilize standard containers and C++11 smart pointers.

    With raw pointers as data members, you would have to maintain The Rule of Three (or The Rule of Five in C++11) because the provided copy constructor and assignment operator will only copy the pointers (shallow copy), not the data they point to (deep copy).

  • Utilize initializer lists for your classes:

    Ball:

     Ball::Ball(int x, int y)
     : status(READY)
     , x(x)
     , y(y)
     , bounce(false);
     , speed(8);
     , angle(0.0f);
     , hits(0); 
     , predicted_y(0)
     {}
    

    Paddle:

     Paddle::Paddle(int new_x, int new_y)
     : x(new_x)
     , y(new_y)
     {}
    

    This will especially be helpful in case you ever need to initialize const members.

  • Paddle's accessors should be const as they do not modify data members:

     int Paddle::get_x() const {
     return x;
     }
    
     int Paddle::get_y() const {
     return y;
     }
    
  • Your randomization initializations should be put into an anonymous namespace:

     namespace {
     std::random_device rd;
     std::mt19937 gen(rd());
     }
    

    This will prevent linker errors in case the same names are used in other files.

  • This:

     int sign;
     if (paddle->get_x() < Pong::SCREEN_WIDTH/2)
     sign = 1;
     else
     sign = -1;
    

    can become a single-line ternary statement:

     int sign = (paddle->get_x() < Pong::SCREEN_WIDTH/2) ? 1 : -1;
    
  • You're using sin() and cos(), but you haven't included <cmath>. I assume your compiler is being lenient for some reason and is not raising errors. You should still include this, as well as prefixing the functions with std::.

  • In collides_with(), I'd condense all the if/else if statements into one if, with each condition separated by a ||. Only one of them has to be met to return false.

    You'll then have something in this form (separate || lines may be necessary):

     if (condition1 || condition2 || condition3)
     return false;
     else
     return true;
    
added 536 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238
  • You don't need return 0 at the end of main(). Reaching that point already implies a successful termination, so the compiler will insert it for you.

  • Prefer to avoid raw pointers and manual memory management whenever possible. Instead, utilize standard containers and C++11 smart pointers. With these current

    With raw pointers as data members, you would have have to maintain The Rule of Three (or The Rule of Five in C++11) because the provided copy constructor and assignment operator will only copy the pointers (shallow copy), not the data they point to (deep copy).

  • Utilize initializer lists for your classes:

    Ball:

     Ball::Ball(int x, int y)
     : status(READY)
     , x(x)
     , y(y)
     , bounce(false);
     , speed(8);
     , angle(0.0f);
     , hits(0); 
     , predicted_y(0)
     {}
    

    Paddle:

     Paddle::Paddle(int new_x, int new_y)
     : x(new_x)
     , y(new_y)
     {}
    

    This will especially be helpful in case you ever need to initialize const members.

  • Paddle's accessors should be const as they do not modify data members:

     int Paddle::get_x() const {
     return x;
     }
    
     int Paddle::get_y() const {
     return y;
     }
    
  • Your randomization initializations should be put into an anonymous namespace:

     namespace {
     std::random_device rd;
     std::mt19937 gen(rd());
     }
    

    This will prevent linker errors in case the same names are used in other files.

  • This:

     int sign;
     if (paddle->get_x() < Pong::SCREEN_WIDTH/2)
     sign = 1;
     else
     sign = -1;
    

    can become a single-line ternary statement:

     int sign = (paddle->get_x() < Pong::SCREEN_WIDTH/2) ? 1 : -1;
    
  • You're using sin() and cos(), but you don't havehaven't included <cmath> included. I assume your compiler is being lenient for some reason and is not raising errors. You should still include this, as well as prefixing the functions with std::.

  • In collides_with(), I'd condense all the if/else if statements into one if, with each condition separated by a ||. Only one of them has to be met to return false.

    You'll then have something in this form (separate || lines may be necessary):

     if (condition1 || condition2 || condition3)
     return false;
     else
     return true;
    
  • You don't need return 0 at the end of main(). Reaching that point already implies a successful termination, so the compiler will insert it for you.

  • Prefer to avoid raw pointers and manual memory management whenever possible. Instead, utilize standard containers and C++11 smart pointers. With these current raw pointers, you would have have to maintain The Rule of Three (or The Rule of Five in C++11).

  • Utilize initializer lists for your classes:

    Ball:

     Ball::Ball(int x, int y)
     : status(READY)
     , x(x)
     , y(y)
     , bounce(false);
     , speed(8);
     , angle(0.0f);
     , hits(0); 
     , predicted_y(0)
     {}
    

    Paddle:

     Paddle::Paddle(int new_x, int new_y)
     : x(new_x)
     , y(new_y)
     {}
    

    This will especially be helpful in case you ever need to initialize const members.

  • Paddle's accessors should be const as they do not modify data members:

     int Paddle::get_x() const {
     return x;
     }
    
     int Paddle::get_y() const {
     return y;
     }
    
  • Your randomization initializations should be put into an anonymous namespace:

     namespace {
     std::random_device rd;
     std::mt19937 gen(rd());
     }
    

    This will prevent linker errors in case the same names are used in other files.

  • This:

     int sign;
     if (paddle->get_x() < Pong::SCREEN_WIDTH/2)
     sign = 1;
     else
     sign = -1;
    

    can become a single-line ternary statement:

     int sign = (paddle->get_x() < Pong::SCREEN_WIDTH/2) ? 1 : -1;
    
  • You're using sin() and cos(), but you don't have <cmath> included. I assume your compiler is being lenient for some reason and is not raising errors. You should still include this, as well as prefixing the functions with std::.

  • You don't need return 0 at the end of main(). Reaching that point already implies a successful termination, so the compiler will insert it for you.

  • Prefer to avoid raw pointers and manual memory management whenever possible. Instead, utilize standard containers and C++11 smart pointers.

    With raw pointers as data members, you would have to maintain The Rule of Three (or The Rule of Five in C++11) because the provided copy constructor and assignment operator will only copy the pointers (shallow copy), not the data they point to (deep copy).

  • Utilize initializer lists for your classes:

    Ball:

     Ball::Ball(int x, int y)
     : status(READY)
     , x(x)
     , y(y)
     , bounce(false);
     , speed(8);
     , angle(0.0f);
     , hits(0); 
     , predicted_y(0)
     {}
    

    Paddle:

     Paddle::Paddle(int new_x, int new_y)
     : x(new_x)
     , y(new_y)
     {}
    

    This will especially be helpful in case you ever need to initialize const members.

  • Paddle's accessors should be const as they do not modify data members:

     int Paddle::get_x() const {
     return x;
     }
    
     int Paddle::get_y() const {
     return y;
     }
    
  • Your randomization initializations should be put into an anonymous namespace:

     namespace {
     std::random_device rd;
     std::mt19937 gen(rd());
     }
    

    This will prevent linker errors in case the same names are used in other files.

  • This:

     int sign;
     if (paddle->get_x() < Pong::SCREEN_WIDTH/2)
     sign = 1;
     else
     sign = -1;
    

    can become a single-line ternary statement:

     int sign = (paddle->get_x() < Pong::SCREEN_WIDTH/2) ? 1 : -1;
    
  • You're using sin() and cos(), but you haven't included <cmath>. I assume your compiler is being lenient for some reason and is not raising errors. You should still include this, as well as prefixing the functions with std::.

  • In collides_with(), I'd condense all the if/else if statements into one if, with each condition separated by a ||. Only one of them has to be met to return false.

    You'll then have something in this form (separate || lines may be necessary):

     if (condition1 || condition2 || condition3)
     return false;
     else
     return true;
    
added 463 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238
  • You don't need return 0 at the end of main(). Reaching that point already implies a successful termination, so the compiler will insert it for you.

  • Prefer to avoid raw pointers and manual memory management whenever possible. Instead, utilize standard containers and C++11 smart pointers. With these current raw pointers, you would have have to maintain The Rule of Three (or The Rule of Five in C++11).

  • Utilize initializer lists for your classes:

    Ball:

     Ball::Ball(int x, int y)
     : status(READY)
     , x(x)
     , y(y)
     , bounce(false);
     , speed(8);
     , angle(0.0f);
     , hits(0); 
     , predicted_y(0)
     {}
    

    Paddle:

     Paddle::Paddle(int new_x, int new_y)
     : x(new_x)
     , y(new_y)
     {}
    

    This will especially be helpful in case you ever need to initialize const members.

  • Paddle's accessors should be const as they do not modify data members:

     int Paddle::get_x() const {
     return x;
     }
    
     int Paddle::get_y() const {
     return y;
     }
    
  • Your randomization initializations should be put into an anonymous namespace:

     namespace {
     std::random_device rd;
     std::mt19937 gen(rd());
     }
    

    This will prevent linker errors in case the same names are used in other files.

  • This:

     int sign;
     if (paddle->get_x() < Pong::SCREEN_WIDTH/2)
     sign = 1;
     else
     sign = -1;
    

    can become a single-line ternary statement:

     int sign = (paddle->get_x() < Pong::SCREEN_WIDTH/2) ? 1 : -1;
    
  • You're using sin() and cos(), but you don't have <cmath> included. I assume your compiler is being lenient for some reason and is not raising errors. You should still include this, as well as prefixing the functions with std::.

  • You don't need return 0 at the end of main(). Reaching that point already implies a successful termination, so the compiler will insert it for you.

  • Prefer to avoid raw pointers and manual memory management whenever possible. Instead, utilize standard containers and C++11 smart pointers. With these current raw pointers, you would have have to maintain The Rule of Three (or The Rule of Five in C++11).

  • Utilize initializer lists for your classes:

    Ball:

     Ball::Ball(int x, int y)
     : status(READY)
     , x(x)
     , y(y)
     , bounce(false);
     , speed(8);
     , angle(0.0f);
     , hits(0); 
     , predicted_y(0)
     {}
    

    Paddle:

     Paddle::Paddle(int new_x, int new_y)
     : x(new_x)
     , y(new_y)
     {}
    
  • Paddle's accessors should be const:

     int Paddle::get_x() const {
     return x;
     }
    
     int Paddle::get_y() const {
     return y;
     }
    
  • Your randomization initializations should be put into an anonymous namespace:

     namespace {
     std::random_device rd;
     std::mt19937 gen(rd());
     }
    
  • This:

     int sign;
     if (paddle->get_x() < Pong::SCREEN_WIDTH/2)
     sign = 1;
     else
     sign = -1;
    

    can become a single-line ternary statement:

     int sign = (paddle->get_x() < Pong::SCREEN_WIDTH/2) ? 1 : -1;
    
  • You don't need return 0 at the end of main(). Reaching that point already implies a successful termination, so the compiler will insert it for you.

  • Prefer to avoid raw pointers and manual memory management whenever possible. Instead, utilize standard containers and C++11 smart pointers. With these current raw pointers, you would have have to maintain The Rule of Three (or The Rule of Five in C++11).

  • Utilize initializer lists for your classes:

    Ball:

     Ball::Ball(int x, int y)
     : status(READY)
     , x(x)
     , y(y)
     , bounce(false);
     , speed(8);
     , angle(0.0f);
     , hits(0); 
     , predicted_y(0)
     {}
    

    Paddle:

     Paddle::Paddle(int new_x, int new_y)
     : x(new_x)
     , y(new_y)
     {}
    

    This will especially be helpful in case you ever need to initialize const members.

  • Paddle's accessors should be const as they do not modify data members:

     int Paddle::get_x() const {
     return x;
     }
    
     int Paddle::get_y() const {
     return y;
     }
    
  • Your randomization initializations should be put into an anonymous namespace:

     namespace {
     std::random_device rd;
     std::mt19937 gen(rd());
     }
    

    This will prevent linker errors in case the same names are used in other files.

  • This:

     int sign;
     if (paddle->get_x() < Pong::SCREEN_WIDTH/2)
     sign = 1;
     else
     sign = -1;
    

    can become a single-line ternary statement:

     int sign = (paddle->get_x() < Pong::SCREEN_WIDTH/2) ? 1 : -1;
    
  • You're using sin() and cos(), but you don't have <cmath> included. I assume your compiler is being lenient for some reason and is not raising errors. You should still include this, as well as prefixing the functions with std::.

Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238
Loading
lang-cpp

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