what to do when 2 constructors of the same class take the same paramaters?

Started by
24 comments, last by C-Junkie 19 years, 3 months ago
hi, i have run into a slight design problem and im curious how you guys would handle it. im working on a 2d online RPG, and im implementing the SMG weapon into the game. anyway, basically, this requires me to put a second constructor in my Bullet class, which takes velocity as a parameter. this is because normally a projectile will calculate its own velocity, however, the SMG Weapon fires a 3 shot burst, and in order to know where to place the 2 trailing bullets, the SMG must calculate the velocity. since i dont want to calculate velocity twice each time an SMG is fired, im going to give my Bullet class a second constructor which takes velocity. anyway, the 2 constructors look like this:
Bullets::Bullets(float start_x, float start_y,float x_vel,  float y_vel, Character *shooter, Uint32 timestamp)
and,
Bullets::Bullets(float start_x, float start_y,float dest_x,  float dest_y, Character *shooter, Uint32 timestamp)
so, are there any clever tricks to make this code compilable? i guess i could just give the first constructor a boolean saying "bool velocity_given", and in all cases set that to true, however, this seems kind of ugly.. thanks for any help.
FTA, my 2D futuristic action MMORPG
Advertisement
Maybe putting the parameter(s) into a struct may help.
well, i guess thats an idea, but IMO, its cleaner to just put the ugly boolean in the first constructor, rather then having to create 2 structs just for this purpose.

thanks for anymore help.
FTA, my 2D futuristic action MMORPG
I've seen velocity represented as a Point structure with an x and y parameter inside of it.

Just out of curiosity why are there changing bullet velocity? I'll be writing similiar projects in the future and am just curious why it isn't constant (not questioning your design just trying to understand why it may be necessary).
bullets can fire in 360 degrees, any direction. velocity must be calculated with each shot.

i dont have a Point class, and dont want to implement one either. thats something that i should have done months ago. but, even then, this would lead to the same problem, unless i did something kind of ugly and had one ctor take a Point and the other take 2 floats.
FTA, my 2D futuristic action MMORPG
so far im thinking the best idea is to just re-order the parameters, e.g., put Character* shooter as the first parameter in the first constructor version. i guess i was just hoping there would be some clever trick for this.
FTA, my 2D futuristic action MMORPG
I would suggest using different functions:
// Needed Data for a bulletBullets::Bullets(float start_x, float start_y,Character *shooter, Uint32 timestamp)void Bullet::SetVel( float xVel, float yVel );void Bullet::SetDest( float destX, float destY );


The other option is to pass *all* the variables into it along with the a bool.

Bullets::Bullets(float start_x, float start_y,float x_vel, float y_vel, float destX, float destY, Character *shooter, Uint32 timestamp, bool calculatesVel = false)


So if you want to create a bullets then:
// Uses Velocity passed in.Bullet B = new Bullet( x,y,xVel,yVel, 0,0 shooter, time );// True means calculate velocity from x/y Pos and x/y DestBullet B = new Bullet( x, y, 0, 0, xDest, yDest, shooter, time, true );
Quote:Original post by graveyard filla
bullets can fire in 360 degrees, any direction. velocity must be calculated with each shot.
Your two constructors are functionally identical. The one takes an explicit (2D) vector velocity, the other takes a (2D) vector direction, from which the velocity is computed. In essence, they both take a vector velocity specification.

Your bullet should be told the direction and speed it should travel at, and nothing more.
Hi,

Quote:Original post by graveyard filla
bullets can fire in 360 degrees, any direction. velocity must be calculated with each shot.
i dont have a Point class, and dont want to implement one either. thats something that i should have done months ago.


Well, you should do it now then. The more you wait, the bigger the problem will be. Points and vectors are rather simple objects, the code is straightforward. You can update older code later - you don't have to make your code Point-class aware at the time you are writing the Point class. Do a progressive change - but fo it.

Quote:Original post by graveyard filla
but, even then, this would lead to the same problem, unless i did something kind of ugly and had one ctor take a Point and the other take 2 floats.


I guess it would bo a better idea. Having a Point class and a Vector class don't hurt and may allow you to write better code:
Bullets::Bullets(const Point& start, const Point& dest,   Character *shooter, Uint32 timestamp){}Bullets::Bullets(const Point& start, const Vector& velocity,   Character *shooter, Uint32 timestamp){}

Using them:
Bullet  bullet0(Point(x,y), Point(xdest, ydest), shooter, ts);Bullet  bullet1(Point(x,y), Vector(xdest-x, ydest-y), shooter, ts);


Quote:Original post by graveyard filla
so far im thinking the best idea is to just re-order the parameters, e.g., put Character* shooter as the first parameter in the first constructor version. i guess i was just hoping there would be some clever trick for this.


Don't do this: having the same parameters in different order may produce ugly code (and therefore eye cancer).

Anyway, Great Old One Oluseyi probably got it right: is there a conceptual (read: not related to implementation) need for these 2 different-but-not-so-different ctors?

Regards,
I would probably create enum types with a single value to use to differentiate constructor calls as so:

enum WithVelocityType { WithVelocity };
enum WithDestinationType { WithDestination };

class Bullet
{
Bullet( WithVelocityType, float x, float y, float dx, float dy, (...) );
Bullet( WithDestinationType, float x, float y, float dest_x, float dest_y, (...) );
};

Bullet b1 = new Bullet( WithVelocity, x, y, dx, dy, (etc.) );
Bullet b2 = new Bullet( WithDestination, x, y, x2, y2, (etc.) );

Then the choice of constructor is still decided at compile-time.

This topic is closed to new replies.

Advertisement