pointer to class members problem

hy,
I'm learnig working with classes and have problem with pointers to members
problem is in comment...
any help please :)

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
class tijelo {
	friend void pomoc (tijelo* ptrObj, bool tijelo::*ptrClan);
public:
	tijelo () : Srce (new bool (true)), LijeviBubreg (new bool (false)), DesniBubreg (new bool (false)),
		Jetra (new bool (true)), Slezena (new bool (false)) {}
	tijelo (const tijelo& reftijelo) : Slezena (reftijelo.Slezena), DesniBubreg (reftijelo.DesniBubreg), LijeviBubreg (reftijelo.LijeviBubreg), 
		Jetra (reftijelo.Jetra), Srce (reftijelo.Srce) {}
	~tijelo () {
		delete Srce;
		delete DesniBubreg;
		delete LijeviBubreg;
		delete Jetra;
		delete Slezena;
	}
private:
	bool* Srce;
	bool* LijeviBubreg;
	bool* DesniBubreg;
	bool* Jetra;
	bool* Slezena;
};

void pomoc (tijelo* ptrObj, bool tijelo::*ptrClan);
int main () {
	tijelo ja;
	tijelo *ptrtijelo = &ja;
	pomoc (ptrtijelo, &ja.DesniBubreg);  //can't access private member!
	system ("pause");
	return 0;
}
//this funct. will test if member has true and then set to false if false do nothing
void pomoc (tijelo* ptrObj, bool tijelo::*ptrClan) {
	if (ptrObj->*ptrClan == false)
		cout << "member is not true!" << endl;

	else {
		ptrObj->*ptrClan = true;
		cout << "member seted to true" << endl;
	}
}

members must be private (so no class changes are allowed in this example-- only function)
I must solve how to make function work.. to acces members and change them.
any one familiar with classes and pointers out there :)
how to make this work?
Well first we have the issue that main function is trying to take
the address of a private member of class tijelo.

So do you want to make main a friend of class tijelo ?? - or put it another way -Would class Tijelo want main function as a friend??
Last edited on
no :/
if main () would be friend of a class then private members will always be accesable from program wriht?

i wann use function "pomoc" not "main" to test if private member has true and set to false that's all
using pointer in that function of course.

is that posible in my code.
or shall I move function pomoc into class.

if I put it into then problem is not solved and friend qualificator has no meaning :/
Last edited on
Topic archived. No new replies allowed.