pointer-to-member data and pointer-to-member functions and access specifiers

S

Stephen Howe

Hi

I having been trying to find if pointer-to-member data and pointer-to-member functions are subject to access specifiers in any way (public, private or protected) and so far have drawn a blank.

Are they subject to or not subject to access specifiers?

Thanks

Stephen Howe
 
Ö

Öö Tiib

Hi

I having been trying to find if pointer-to-member data and pointer-to-member
functions are subject to access specifiers in any way (public, private or
protected) and so far have drawn a blank.

Are they subject to or not subject to access specifiers?

I maybe do not understand what you ask. I see no difference between pointer
to member type and some other type.

These are types. You can not modify access to such types. If
class type is visible then types of pointers to its instances and
members are also visible as types.

When you declare a member variable of that type (like any type) private
then it is accessible only privately. If public member function returns a
value of that type (like any type) then it is usable publicly.
 
S

Stephen Howe

Are they subject to or not subject to access specifiers? The question is not too clear, but maybe you are confusing compile-time with run-time?

No, I meant compile-time. It has become clear to me.
An example
class B
{
public:
void bset(int val) { bval_ = val; }
private:
void bset2(int val) { bval_ = val; }
private:
int bval_;
};

int main()
{
B b;

void (B::*f1)(int) = &B::bset;
(b.*f1)(7);
void (B::*f2)(int) = &B::bset2; // LINE 19
(b.*f2)(8);

return 0;
}
Line 19 above will fail to compile, because bset2 is private and main() has no special priviledges to set f2 to point to bset2.
It amounts to the fact that pointers-to-member functions when being initialised or assigned are subject to the same rules governing public/private/public access.

And if the example above had pointers-to-member data, again public/private/public access rules also apply.

Thanks

Stephen Howe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top