Jump to content
  • Advertisement
Sign in to follow this  
graveyard filla

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

This topic is 5078 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
I would suggest using different functions:

// Needed Data for a bullet
Bullets::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 Dest
Bullet B = new Bullet( x, y, 0, 0, xDest, yDest, shooter, time, true );



Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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,

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!