Passing a class's member variable or function as an argument to a function

Hi,

I need a little help, how can I, if possible, pass a class's member function to a regular function?

I'm trying to make a function that accepts class's member variable (health) as an argument, and then it will check to see if the enemy is dead or not, and then it will delete the allocated space that that enemy object is taking up if the health is zero or lower.

heres my code so far:
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include "Enemy.h"
#include "Soldier.h"
#include "Beast.h"
using namespace std;









int main()
{

	Soldier* SoldrNME = new Soldier();
	SoldrNME->getStats();
	cout << endl;

	SoldrNME->takeDamage(3);
	cout << endl;
	SoldrNME->getStats();
	cout << endl;
	//*******************************************************************
        //this is the if statement that will make up the deathCheck function
        //******************************************************************
	if (SoldrNME->getHealth()<= 0)
	{
		cout << SoldrNME->getName() << " has been killed\n";
		delete SoldrNME;
	}


	cout << endl << endl;
	SoldrNME->takeDamage(15);
	cout << endl;;
	SoldrNME->getStats();
	
        //*******************************************************************
        //this is the if statement that will make up the deathCheck function
        //******************************************************************
        if (SoldrNME->getHealth()<= 0)
	{
		cout << SoldrNME->getName() << " has been killed\n";
		delete SoldrNME;
	}





	return 0;
}

 void deathCheck(//what should I put here?)
{
//******************************************************************
//I will change the names accordingly, I just need to get a basic
//Idea on how to do this,I feel that rewriting the if statement is a
//waste of space and time, and should at least try to incorporate it 
//into a function
//******************************************************************

        if (SoldrNME->getHealth()<= 0)
	{
		cout << SoldrNME->getName() << " has been killed\n";
		delete SoldrNME;
	}

}


Any help would be great, Thanks in advance
how can I, if possible, pass a class's member function to a regular function?

I'm not sure what you mean by that. It doesn't look like you need to pass a function to accomplish what you want. Have you tried passing the pointer as an argument?
void deathCheck(Soldier* SoldrNME){

By the way, be careful not to use SoldrNME after you've deleted it.
1
2
3
4
5
6
7
8
9
if (SoldrNME->getHealth()<= 0)
	{
		cout << SoldrNME->getName() << " has been killed\n";
		delete SoldrNME;
	}


cout << endl << endl;
SoldrNME->takeDamage(15);

This is dangerous. If you delete the pointer and then call takeDamage() on it, bad things may happen.
Perfect!

That's exactly what I was looking for, thanks a lot.
And thanks for the heads up on not using an already deleted
SoldrNME
Topic archived. No new replies allowed.