Sign in to follow this  

Program Design question on classes.

This topic is 4339 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 all, I think I have this one figured out, but I'm looking for some experienced advice on whether or not I have this right. What my problem is, is this: I am creating several Players each with their own sets of data. During gameplay some actions require that I access data from Players other than the active one. Now, where my problem lies is I don't know how many Players will be present now which Player I will need to access data from until it occurs. As such, In my program's present form I can't access the data readily. I.e. Tonight I ran into a problem where Player contains a vector<unit> playersUnits, and in unit I have a method attackOpponent(attackerUnit, defenderUnit) But I don't know who defenderUnit is ahead of time, and can't access the defenderUnit from within the attacking Player's vector<unit>(attackOpponent). What I'm thinking is the correct solution is to start gameloop() and initialize the game variables such as number of players, then start the while loop passing the game variables and number of players to a parent class Player which then creates subclasses of players and my other classes all inherited from the parent class Player, allowing me full access to all Players and their data/objects freely by having all the players exist as a vector and easily referenced as a vector. Then all interactions in the while loop occur in Player and the subclasses instanced within it. That way in my parent classes instance of unit I can call attackOpponent and pass it the units stored in the subclass vectors. Is this correct, or should I be pursuing it through the use of namespaces? Thanks in advance!

Share this post


Link to post
Share on other sites
A little more information would be helpful.

Is your issue that when you call attackOpponent, defenderUnit is null or that you can't access the methods, properties, etc associated with defender unit.

Share this post


Link to post
Share on other sites
It is very difficult to understand what you wrote. Perhaps you might clarify.

If I understand you correctly, the solution is to use pointers. Also, it is not a good idea for a base class to create and/or manage derived classes.

Share this post


Link to post
Share on other sites

This topic is 4339 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