pointer to class members (need advice)

hi, I'm learni about pointers to class members and metods and i've stuck on part where members are private:

MyClass.h
1
2
3
4
5
6
7
8
9
10
11
#pragma once
class myClass {
	friend void Helper(MyClass* object, bool MyClass::*pointer);
private:
	bool tip1;
	bool tip2;
	bool tip3;
	bool tip4;
public:
	myClass () : tip1(false), tip2(false), tip3(true), tip4(false) {}
};


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "MyClass.h"
#include <iostream>
using std::endl;
using std::cout;

void Helper(MyClass* object, bool MyClass::*pointer);

int main() {
        MyClass NewObject;
        MyClass* pointer = &NewObject;
	bool MyClass::*ptr = &MyClass::tip3; //tip3 is inaccesable
	Helper(pointer, ptr);  
        return 0;
}
void Helper(MyClass* object, bool MyClass::*pointer) {
	if(object->*pointer == true) {
		cout << "now is false" << endl;
		object->*pointer = false;
	}
	else
		cout << "it was not true" << endl;
	return;
}


so this code works only if members are public,
I dont know how to make this code work now when mebers are private.
I have tried to use member function which will return address of member but that does not work.

please advice me :/
Private members are only visible from their classes, so the code works as expected. You can make them visible or create get functions in MyClass in order to acces its values:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#pragma once
class myClass {
	friend void Helper(MyClass* object, bool MyClass::*pointer);
private:
	bool tip1;
	bool tip2;
	bool tip3;
	bool tip4;
public:
	myClass () : tip1(false), tip2(false), tip3(true), tip4(false) {}
	bool getTip1() { return tip1; }
	bool getTip2() { return tip2; }
	bool getTip3() { return tip3; }
	bool getTip4() { return tip4; }
};
hy tnx for your reply,
I've done so and here is compiler output:
 illegal call of non-static member function


1
2
3
4
5
6
7
int main() {
        MyClass NewObject;
        MyClass* pointer = &NewObject;
	bool MyClass::*ptr = MyClass::getTip3(); // illegal call of non-static member function
	Helper(pointer, ptr);  
        return 0;
}


I have tryed returnig by reference to but same error

bool& getTip3() { return tip3; }
Last edited on
If it where me I would do something like this presuming you have written your getters.

MyClass* pointer = new MyClass();
pointer->getTip3();

//or

MyClass* pointer = new MyClass();
bool b = pointer->getTip3();

This would assign b the value of tip 3.

Hope this helps or at least steers you in the right direction.
Why are you using = MyClass::getTip3(); now rather than the = &MyClass::getTip3; you used earlier?

You are trying to init ptr to the address of the member function, rather than call a static function?
Last edited on
hi,
thanks for your reply!

the thing which I'm trying to learn is using poiner to class meber in this format:
bool MyClass::*ptr = &MyClass::tip3; this works for public members
a task from my book is asking me to create the same thing for private members..
that's the main problem.
I cant find any article on the net on how to do that(using this format)
MyClass* pointer = &NewObject; and this is the first argument of the function which works.

and this function should use that pointer: (that's why I need such pointer type)
1
2
3
4
5
6
7
8
9
void Helper(MyClass* object, bool MyClass::*pointer) {
	if(object->*pointer == true) {
		cout << "now is false" << endl;
		object->*pointer = false;
	}
	else
		cout << "it was not true" << endl;
	return;
}
you cant use :: on a private class member. You have to use the get function that you have created.
heh thanks to all who tryed to help me!!

you cant use :: on a private class member. You have to use the get function that you have created.

I vigured out according to your comment to use pointer to member function and use him to get status of the "tip" !!

a task in my book was a TRICK "use a pointer to class member" (metod accualy! LOL)
here is solved solution which works now:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#pragma once
class myClass {
	friend void Helper(MyClass* object, bool (MyClass::*ptr_to_func)());
private:
	bool tip1;
	bool tip2;
	bool tip3;
	bool tip4;
public:
	myClass () : tip1(false), tip2(false), tip3(true), tip4(false) {}
	bool getTip1() { return tip1; }
	bool getTip2() { return tip2; }
	bool getTip3() { return tip3; }
	bool getTip4() { return tip4; }
        void set_tip3(bool status) {tip3 = status;}
};

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "MyClass.h"
#include <iostream>
using std::endl;
using std::cout;

void Helper(MyClass* object, bool (MyClass::*ptr_to_func)());

int main() {
        MyClass NewObject;
        MyClass* pointer = &NewObject;
	bool (MyClass::*ptr_to_func) () = &MyClass::getTip3;  // POINTER TO FUNCTIN RETURN STATUS
	Helper(pointer, ptr_to_func);  
        return 0;
}
void Helper(MyClass* object, bool (MyClass::*ptr_to_func)()) {
	if(object->*ptr_to_func == true) {
		cout << "now is false" << endl;
		object->*ptr_to_func = false;
	}
	else
		cout << "it was not true" << endl;
	return;
}


THANKS!!!
Last edited on
Topic archived. No new replies allowed.