Passing a passed pointer

Hey guys so Im able to pass my Class Object through functions like so

1
2
3
4
void startFight(Player *p) {
    p->attack(); 
    p->takeDamage(); 
} 


thats fine and great but what if I wanted to pass that already passed pointer obj again like so.

1
2
3
4
5
6
7
8
9
10
11
12
13
14

void startFight(Player *p) { 
   p->attack(PASSED HERE); 
}

// attack now uses a player obj as well 
void attack(Player *p) {
    // do attack stuff 
} 

int main() {
    Player p; 
    startFight(&p); 
}


Thanks guys. If this isn't possible lemme know and give me solutions to what to do in this case :) Sorry if these functions don't make sense for passing objects to its just an example of what im trying to achieve function wise.


EDIT:

heres the class thats doing this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 
// levelOne(Player *p) - this function loads level one for the player that is loaded 
// into the level 
void levelOne(Player *p) {
    std::cout << "Level 1\n"; 
    std::cout << "---------\n"; 
    std::cout << "Welcome to the game " << p->getID() << '\n';
    std::cout << "of class type " << p->getPlayerClass() << '\n';
    std::cout << "were gonna start you off in a random battle sir.\n";
    std::cout << "of course to be safe and keep me out of trouble Im gonna change your HP by a lot\n";
    int classHP = p->getHealth(); 
    p->setHealth(500); // set their HP to 500 
    std::cout << "before we start, take a look at your starting stats and see how much HP I gave you: \n"; 
    p->getStats(); 
    std::cout << "thats you, a brilliant " << p->getPlayerClass() << " of your kind.\n"; 
    std::cout << "now lets get ready for some battle!\n"; 
    std::cout << "follow the instructions and good luck!\n"; 
    
    playerFight(p); // PASSING POINTER OBJ HERE 
    
    p = nullptr; 
}


and playerFight looks like

1
2
3
void playerFight(Player *p) {
     //fighting stuff 
}


curently not getting any errors for playerFight(p); but Im curious if this is still properly passing the player obj.
Last edited on
void attack(Player *p) { ... }
and
void Player::attack(Player *p) { ... }
are completely different functions, that just happen to be called the same thing and take in the same parameter type (Player*).

If you want to call the function you defined on line 7 (line 15 in my code), do something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Example program
#include <iostream>
#include <string>

class Player {};


void attack(Player*); // function prototype, so that the compiler knows what attack is

void startFight(Player *p) { 
   attack(p); 
}

// attack now uses a player obj as well 
void attack(Player *p) {
    // do attack stuff 
} 

int main() {
    Player p;
    startFight(&p); 
}


If you want to do this in an "object-oriented" way, you could design your code like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Example program
#include <iostream>
#include <string>

class Player {
  public:
    void startFight();
    void attack();
};

void Player::startFight()
{
    attack();   
}

void Player::attack()
{
    // do attack stuff   
    std::cout << "Attacking" << std::endl;
}

int main() {
    Player p;
    p.startFight();
}
Last edited on
Okay thanks man :) from looking at my edit am I doing this right?
Last edited on
I am not sure you have a solid design for what you are trying to do.

In your sample code, line 7 declares a non-member function void attack(Player *p). So, your call to this function in line 3 should not be a member function on a Player. In other words, line 3 should simply be attack(p);

Now, if you are trying to say that 1 Player attacks another Player, then you need to have 2 Player objects. Then you could have a member function something like this:

1
2
3
4
5
class Player
{
public:
    void attack(Player *other);
};


Then you could call this something like this:

1
2
3
4
void startFight(Player *firstPlayer, Player *secondPlayer)
{
    firstPlayer->attack(secondPlayer);
}


It all depends on what you are trying to do.

By the way, I think passing Players around would be easier if you made them references rather than pointers. The above code would be:

1
2
3
4
5
6
7
8
9
10
class Player
{
public:
    void attack(Player &other);
};

void startFight(Player &firstPlayer, Player &secondPlayer)
{
    firstPlayer.attack(secondPlayer);
}
@bradltr95
Looks okay. However, in C++, you can use references rather than pointers in many places.

Also, please just make a new post instead of editing the OP. It makes it harder for others to follow.

The C way of doing things is something like this:
1
2
3
4
void levelOne(Player* player) // non-member function, passing Player by pointer.
{
    player->setHealth(500); // member function
}


In C++, you can pass by reference, or use member functions.
1
2
3
4
void levelOne(Player& player) // non-member function, passing player by reference
{
    player.setHealth(500); // member function
}
Last edited on
While were on this topic, should I be deleting the passed obj reference? And how?
when I try to

1
2
3
void func(Player *p) {
delete p; 
}


I get tons of errors. When and where do I delete.

To answer ur other questions. attack and startFight are all within the main.cpp I didnt put them in the Player class. so thats why Im passing the object to them so i can edit player and enemy properties

Last edited on
You only delete something if you created it using new.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Player {};

void do_stuff(Player* player)
{

}

int main()
{
    Player player; // does not need to be deleted, has automatic storage duration

    Player* p2 = new Player; // needs to be deleted manually

    do_stuff(&player);
    do_stuff(p2);

    delete p2;
}


In general, prefer automatic storage duration (NOT using new/delete) unless you have a good reason.
Last edited on
Oh wow okay thanks this will be useful in the project since I create the Enemy e;'s on spot :)
Topic archived. No new replies allowed.