Ash
Ash

Reputation: 183

C++ passing object to functions

Question:

What syntax do you use to have two objects of the same class interact with each other in their respective functions?

I have two objects in the class soldier. The class function attack(y) takes the current_hp int of of one object and subtracts the atk int value of the other object. I'm not able to pass object names through the arguments like this. Obviously it's not working, and the references further in the code won't work either.

Background:

I've been going through online lessons and came across a challenge that should be appropriate to the lessons I've had. I did it without issue but I went back and rewrote it keeping in mind functions and classes.

I can't find anywhere the information to help me so I came here. I might be searching for the wrong thing but... new to this any help would be appreciated.

class soldier{
private:
    int curent_hp = 0;
    int atk = 0;
    int init = 0;
public:
    bool attacked_yet = false;
    void create_soldier(){
        curent_hp = rand() % 20 + 1;
        atk = rand() % 5 + 1;
        init = rand() % 100 + 1;
    }
       int attack(y){
 // the y here being the one that is being attacked
        int attackroll = rand() % 100 + 1;
        if (attackroll < 30){
            y.curent_hp = y.curent_hp - atk;
            }
    }
};

Upvotes: 0

Views: 90

Answers (2)

2785528
2785528

Reputation: 5576

What syntax do you use to have two objects of the same class interact with each other in their respective functions?

and from Google, 'syntax' is defined as

... the arrangement of words and phrases to create well-formed sentences in a language.

In this context, I'm not sure what you mean by syntax.


As a starting point, and something to write about,

  • in telecomm embedded systems, I dealt with working/protect pairs, all in C++.

hardware context:

  • Almost all work/protect hw is exchangeable ... it was ok to simply swap the cards slots.

Software context:

  • The work and protect cards were "...two objects of the same class."

Work and protect cards cooperated by hw for 'fast' response, and software discovered any actions (take by the hw) within the next second.


Work / protect has implications to the ctor:

a) Both cards are constructed without assumptions about the other's presence.

Note: if the other is never installed, the system would still work, just without the 'recovery' behavior.

Roughly similar to how two soldiers would never fight when only 1 of them existed.

b) When the controller code is ready to create the second (of a pair), it knows that the first work/protect card is ready. It simply constructs the second, and then passes a reference into the latest construct, about the first sibling.

c) Upon receipt of the sibling reference, the second object of the two would update both instances. I usually gave the reference to the second card immediately after the second card ctor had completed (without errors).

Note: In C++, sibling instances of the same object class have full access to the other, when provided a reference or pointer.


I also contributed to battle simulation effort (prior to telecomm).

Further interactions (of a battle) can be taken individually by each soldier, if your controller provides each soldier with the stimulus, i.e. invoke a function.

You also may find that, upon receiving a stimulus, it may be easier when one soldier instance takes a single stimulus related action on both soldier instances.


In a battle sim effort I worked on some time ago, all sim actions were stimulated by weapon fire (and, in that case, a laser signal). Distance and weapon type (tank vs ar15) influenced the lethality when hit (as detected by laser sensor).

That software was greatly simplified because every participant (and node in the sim) was both a weapon AND a target.

Good luck in your effort.


Probably final comment:

I generally do not pass objects to functions. Instead I invoke functions of objects. There are many articles about this issue. I can recommend:

see: https://pragprog.com/articles/tell-dont-ask

see also: https://martinfowler.com/bliki/TellDontAsk.html Tell-Don't-Ask is a principle that helps people remember that object-orientation is about bundling data with the functions that operate on that ... Martin Fowler

Upvotes: 0

ravenspoint
ravenspoint

Reputation: 20615

You need to pass a reference to the soldier being attacked, like this:

/** Attack a target soldier
@param[in] target soldier to be attacked
*/
int attack( soldier& target )
{
  ...

By the way, do not use parameter names like y, unless you are working on geometric problems with x, y and z co-ordinates. Meaningful parameter names save a lof of grief!

Upvotes: 3

Related Questions