Sign in to follow this  

Class A uses Class B, and B uses A (in separate Files) problem

This topic is 3600 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, im having some problem wih my classes, class A has a pointer type B. class B has an object to of type A,
//FILE A.h
Class A{
	public:
		B* pb;
};

//FILE B.h
Class B{
	public:
		A objecta;
};
they are in different files, i've tried adding class B; before defining class A but i didnt work, because they are in separate files.

Share this post


Link to post
Share on other sites
If this situation occurs, it usually marks the time to re-examine your class structure. Ask yourself why some functionality needs to be in B when A needs to use it? Why not put it in A anyways?

Happy coding!

Share this post


Link to post
Share on other sites
Quote:
Original post by LordShade
If this situation occurs, it usually marks the time to re-examine your class structure.


There are plenty of good reasons, not the least of which is containment with back-references. That is, some class A which has a list of B and each B only has one A, so you should be able to also navigate from the B back to A.

Share this post


Link to post
Share on other sites
Quote:
Original post by LordShade
If this situation occurs, it usually marks the time to re-examine your class structure. Ask yourself why some functionality needs to be in B when A needs to use it? Why not put it in A anyways?

Happy coding!



yes, you are right about that im re-examining the need of the object A atribute from class B

in my case one class is CProjectile (this would be class A)
that has a pointer to the CWeapon(this would be class B) that belongs to, so when doing the motion, collision and physics algorithms will be easier to deal cause i can grab all existing projectiles and recalculate them in relation to the weapon it was fired from.

up to this point, most of you should agree with the logic.

now comes the optional part, which i still dont know if i would need it for future:
until now CWeapon class had an array of objects CProjectile, a gun has projectiles so at first one should think that a weapon should have that array.
(update: like as SnprBoB86 said, to navigate from a to b and from b to a.)
but after thinking twice maybe is better to just not have any array and have a global one, i would loose B to A but still have A to B. i will see whats better over these days.
thanks

Share this post


Link to post
Share on other sites
I get what you're using it for. Ask yourself, why should a projectile need to know what it is fired from? In the real world, a 9mm fired from any pistol or submachine gun just knows that when it's primer is triggered, it ignites the powder and launches the round at some arbitrary velocity. The gun itself determines it's vector and spin.

So really, the round shouldn't know anything about the weapon it is fired from. The weapon should just set the vector and modify speed if necessary.

Pre-caching projectile classes will speed up your performance as you mentioned.

I'm not saying that it is wrong to have classes linking to each other as mentioned, all I'm saying is that more often than not, there is a better solution.

Happy coding.

Share this post


Link to post
Share on other sites
Quote:
Original post by LordShade
I get what you're using it for. Ask yourself, why should a projectile need to know what it is fired from? In the real world, a 9mm fired from any pistol or submachine gun just knows that when it's primer is triggered, it ignites the powder and launches the round at some arbitrary velocity. The gun itself determines it's vector and spin.

So really, the round shouldn't know anything about the weapon it is fired from. The weapon should just set the vector and modify speed if necessary.

Pre-caching projectile classes will speed up your performance as you mentioned.

I'm not saying that it is wrong to have classes linking to each other as mentioned, all I'm saying is that more often than not, there is a better solution.

Happy coding.


yes, but you know that linking the projectile to the weapon, you can make wonderful creations like boomerang style bullets, rockets that orbit around you changing dinamically, energy rails that connect you and the target at every moment, well lots of intelligent projectiles and of course the classical bullets that you mention dont get affected by this structure.

Share this post


Link to post
Share on other sites
that is another option you are right.
ill keep the object pointer until i realize i wont need the other functions/atributes at all.

auoheoeahoauehauoehao!

Share this post


Link to post
Share on other sites

This topic is 3600 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this