[SDL] Spliting my code into many *.h and *.cpp files problems.

Started by
62 comments, last by Spudder 19 years, 6 months ago
It's just an example, you don't have a class CBall so don't worry about it too much, you can just replace CBall with whatever class name you are using to represent your ball.

Basically you are taking a reference as the parameter is of a user defined type so passing the object by reference is more efficient than by-value. You specify the paramater const(ant) because you are not going to be modifying the object - if you passed by value then the const specifier don't matter as you will only be modfying a copy of the original object.
Advertisement
I still can't understand how that function works. Is it (const MyBallClass &MyBallClassInstance)? If i pass that into this paddle function what will it let me do with the ball class? 0_o




(I knewi should have learned more before making games)
With that function you are allowed access to any members/functions inside the class according to their access specification - public/private etc.

What you aren't allowed to do is to alter the state of the object by either directly modifying the member variables (if they are public) or indirectly by calling non-const member functions which themselves may alter the internal state of the object.
But how am i going to make it see the classes _Ball instance ball? It is only initialized in the main.cpp file while the descriptions of paddles functions are in the paddles.cpp.
Stick the class declaration in a separate .h/.cpp file. Then all you need to do is #include the header file for you to have access to the class.
Ok, ive done what you said, i started writing the function:
void paddles::CheckBallCol(const _Ball &ball) {	if (ball.Get_ypos()>ypos || ball.Get_ypos()<(ypos+150)) {	// Reverse velocity and speed;	}};

but im getting this error(2 of them):
error C2662: 'Get_ypos' : cannot convert 'this' pointer from 'const class _Ball' to 'class _Ball &'
Conversion loses qualifiers
Whats wrong?
*bump*
That error is because you are calling a non-const method on a const object.

Don't panic though, providing in the body of Get_ypos() you don't make any changes to the internal state of the class you can declare them const. To do this all you need to do is to place const after the function declaration like so:

//declarationint Get_ypos() const;//definitionint CBall::Get_ypos() const{  //blah}


In this situation seeing as you are going to need to alter the ball objects attributes you aren't going to be able to pass the ball object by a const-reference, instead just pass it by refernece and remove the const qualifier from the parameter.

Sorry for any confusion, I forgot you are going to need to modify the object in the collision detection when I originally posted about passing by const-reference.
Ok, ive got it to work, but i have some questions, this is what i have right now:
void paddles::CheckBallCol(_Ball &ball) {if (ball.Get_xpos()<10) {  if (ball.Get_ypos()>ypos || ball.Get_ypos()<(ypos+150)) {	ball.change_speed(-ball.Get_speed());	ball.change_velocity(-ball.Get_velocity());  }}	else {	}};

It works, but it seems as its not a very OO thing to do, what if i had more than 1 ball. I need some kind of way to tell it if its the 1st balls instance or the second. Also, How do i make it that it would check collision diffrently for Player paddle and computer paddle? Right now if i would call this function from players paddle instance it would do everything when the ball hits the player paddle, but if i would call it from the computers paddles instance it would do the exact same thing.
You can always add a variable inside the paddle class to keep tracj of the paddle's owner. Then in your collision detection code you can simply check the value of this variable and act accordingly.

I don't really think you need to worry about that, each instance of the paddle class will have it's own set of data completely independant of each other so when you check for collision using the computer's paddle you will be checking the balls position against the computer's paddles position and likewise for the player paddle.

This topic is closed to new replies.

Advertisement