Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange bug with default constructor (C++)

First of all, I'd like to apologize in advance if the answer is obvious; I am very new to C++ and my first language is Java. I am also new to Stack Overflow, so if something is wrong with my question or you need anything else, please tell me.

So. I have this piece of code here: (I am using SFML for the vector and the CircleShape)

Ball::Ball() {

    // This ugly thing calls the full constructor with a random x and y position
    // in such a way the the entire ball is inside the screen.

    Ball::Ball((rand() % (WINDOW_X - (2 * BALL_RADIUS))) + BALL_RADIUS, (rand() % (WINDOW_Y - (2 * BALL_RADIUS))) + BALL_RADIUS);

}
Ball::Ball(float x, float y) {

    loc.x = x;
    loc.y = y;

    ball.setPosition(loc.x - BALL_RADIUS, loc.y - BALL_RADIUS);
    ball.setRadius(BALL_RADIUS);
    ball.setFillColor(sf::Color::Red);
    ball.setOutlineColor(sf::Color::Black);
    ball.setOutlineThickness(1);

}

And here is the header (#included into the above file):

class Ball {

private:
    sf::CircleShape ball;
    sf::Vector2f loc;
    sf::Vector2f vel;
    sf::Vector2f acc;

    void update();
    void bounce();
    void draw();

public:
    Ball();
    Ball(float x, float y);
    void run();

};

When I create the ball with

Ball ball;

(and yes, all of the SFML rendering stuff works), it never shows. A bit of investigation shows that its loc.x and loc.y variables are not set, and probably, neither are the radius, fillcolor, etc of the ball object. If I print the values of these with std::cout inside the constructor, loc.x and loc.y and all the others are set, so I assume that they get unset somewhere after the constructor. What is strange is that if I create the ball with

Ball ball((rand() % (WINDOW_X - (2 * BALL_RADIUS))) + BALL_RADIUS, (rand() % (WINDOW_Y - (2 * BALL_RADIUS))) + BALL_RADIUS);

or even

Ball ball(400, 300);

everything works perfectly, and the ball shows up on-screen. I am really stumped guys. If anyone could help me, that would be great.

BTW, I am running OS X 10.8 with Xcode 4.5.2, and using SFML RC2.0, if that makes any difference.

Thanks,

Matt

like image 598
SlEePlEs5 Avatar asked Feb 20 '13 12:02

SlEePlEs5


1 Answers

Calling a constructor from another constructor (known as delegating a constructor) was impossible before C++11. To do it in C++11, you need to use the member initialisation list:

Ball::Ball()
 : Ball((rand() % (WINDOW_X - (2 * BALL_RADIUS))) + BALL_RADIUS,
        (rand() % (WINDOW_Y - (2 * BALL_RADIUS))) + BALL_RADIUS)
{ }

Pre-C++11, you can create another function that does the common work and get both constructors to call it.

Ball::Ball() {
  init((rand() % (WINDOW_X - (2 * BALL_RADIUS))) + BALL_RADIUS,
       (rand() % (WINDOW_Y - (2 * BALL_RADIUS))) + BALL_RADIUS);
}

Ball::Ball(float x, float y) {
  init(x, y);
}

void Ball::init(float x, float y) {
  loc.x = x;
  loc.y = y;

  ball.setPosition(loc.x - BALL_RADIUS, loc.y - BALL_RADIUS);
  ball.setRadius(BALL_RADIUS);
  ball.setFillColor(sf::Color::Red);
  ball.setOutlineColor(sf::Color::Black);
  ball.setOutlineThickness(1);
}
like image 107
Joseph Mansfield Avatar answered Sep 23 '22 16:09

Joseph Mansfield