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

Discussion in 'C++' started by Stephen Howe, Nov 4, 2012.

  1. Stephen Howe

    Stephen Howe Guest

    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
    Stephen Howe, Nov 4, 2012
    #1
    1. Advertising

  2. Stephen Howe

    Öö Tiib Guest

    On Monday, 5 November 2012 01:24:22 UTC+2, Stephen Howe wrote:
    > 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.
    Öö Tiib, Nov 5, 2012
    #2
    1. Advertising

  3. Stephen Howe

    Stephen Howe Guest

    >> 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
    Stephen Howe, Nov 6, 2012
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steven T. Hatton

    access specifiers and memory layout

    Steven T. Hatton, Jul 10, 2005, in forum: C++
    Replies:
    10
    Views:
    568
    Steven T. Hatton
    Jul 10, 2005
  2. Tapeesh
    Replies:
    8
    Views:
    311
    alpha_beta_release
    Jan 13, 2006
  3. Rahul
    Replies:
    3
    Views:
    393
    Salt_Peter
    Jan 8, 2008
  4. Sourav

    access specifiers

    Sourav, Sep 21, 2006, in forum: Ruby
    Replies:
    3
    Views:
    104
    Sourav
    Sep 21, 2006
  5. somenath
    Replies:
    10
    Views:
    247
    James Kanze
    Jul 2, 2013
Loading...

Share This Page