[OOP] Two classes that can acces each other's members
#1 Members - Reputation: 126
Posted 28 July 2012 - 04:26 PM
Suppose I have two classes : class A and class B .
In my program , A needs to acces some of B's members but B also has to acces some of A's members.
If it's possible, how do I implement this in C++ ?
A and B are separate entities.I do not want to include one of them as a member of the other one .
#4 Members - Reputation: 126
Posted 28 July 2012 - 06:14 PM
The enemy's AI is implemented as a state machine. By default , he leaves the player alone.
The player can however trigger the "Attack" stance of the enemy, but for that he needs to read some info from the enemy object(the enemy position for example).
In order to run the "Attack" stance, my enemy also has to read the player object's informations (like player position and speed for example);
Also, he must modify the player's speed at begining of the "Attack" stance and set it back after the "Attack" finishes.
I've adopted this method because it seems more logical to me. Only the enemy knows when he stops the attack and its reasonable that he reinitializes the player's speed too.There's no point in querying the player object
" Are you in a "chased" stance ? " at each iteration, at least I dont think so.
@Madhed
C.How do I do that ?
From what I know , if A is a friend of class B, then there must be an object of type B in A's declaration so that A can acces the private B's stuff.
I used to classes just for this example.In reality , the player must acces the members of a second class too, C containing the obstacles .
Edited by cold_heats_.--., 28 July 2012 - 06:16 PM.
#5 Members - Reputation: 245
Posted 28 July 2012 - 06:58 PM
[source lang="cpp"]class A;class B{ friend class A; int x; // can be any data type, I chose int for simplicity public: void do_stuff(A a_object); // some function that acts on an A's private members};class A{ friend class B; int y; public: void do_stuff(B b_object);};[/source]
Now you can do something like:
[source lang="cpp"]B::do_stuff(A a_object){ a_object.x += 1;}A::do_stuff(B b_object){ b_object.y += 1;}[/source]
#6 Crossbones+ - Reputation: 962
Posted 28 July 2012 - 08:10 PM
Edited by Mussi, 28 July 2012 - 08:12 PM.
#7 Members - Reputation: 172
Posted 29 July 2012 - 01:10 AM
"Combatant" has speed, position, possibly weapons, inventory, etc..
Then "Player" is a class derived from "Combatant" - uses input (keyboard,mouse, etc.) to change animations, positions, etc.
And "Enemy" is also class derived from "Combatant" - but this uses AI to drive it...
This would also allow later for AI characters to fight on either side, or possibly have multiple factions...
And networked / LAN players could be derived as well...
#8 Members - Reputation: 172
Posted 29 July 2012 - 01:12 AM
So - if any combatant needs info on another combatant, it just accesses the base-class ( or higher class through an interface / virtual function )..
No combatant knows or cares what is "brains" - only if they are "friend" or "foe" or if they're attacking, etc.. etc..
#9 Members - Reputation: 1245
Posted 29 July 2012 - 05:28 AM
Why don't you introduce another layer as Mussi has suggested. A system that keeps track of object positions, where the Enemies can create triggers that get executed when a player enters. That way the logic is a level higher than the individual players or enemies and the enemies just get notified when a player enters their field of view.
#11 Members - Reputation: 126
Posted 29 July 2012 - 08:41 AM
It seems I missed a lot.It would have never occured to me to try an idea like hstubb's one.
I guess I'll try for the moment Mussi's first suggestion.
Thanks for the time to reply
#12 Members - Reputation: 211
Posted 29 July 2012 - 09:22 AM
Make a base class , say Entity ( controls all types of movable objects) , and have Player inherit this Entity class, and your Enemy inherit the class... so both are a Entity, and share the basic structure of an Entity but have special functions related to themselves.
I am not too comfortable with OOP myself, but that would be something I would experiment with as a attempt to see if it solves my issues.
#13 Members - Reputation: 382
Posted 29 July 2012 - 05:18 PM
A. Make all members public
This is very wrong. C++ allows you to do that, but you will create a strong relationship between A and B that may cause you greater problems in the future. Besides, those classes won't be easily reused throughout your code or other projects.
Try looking a little into OO basics and search for some Design Patterns like MVC first.
#15 Members - Reputation: 239
Posted 30 July 2012 - 02:18 PM
Maybe I don't understand the question properly.
#16 Members - Reputation: 126
Posted 31 July 2012 - 04:28 AM
http://imgur.com/aez1h
The dots represent pointers .Order of declaring classes is bottom up .
Tell me , how bad is it ..
#17 Crossbones+ - Reputation: 962
Posted 31 July 2012 - 08:16 AM
Edited by Mussi, 31 July 2012 - 08:17 AM.






