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. Advertisements

  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. Advertisements

  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. Advertisements

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. Fraser Ross
    Replies:
    4
    Views:
    1,314
    Fraser Ross
    Aug 14, 2004
  2. Steven T. Hatton

    access specifiers and memory layout

    Steven T. Hatton, Jul 10, 2005, in forum: C++
    Replies:
    10
    Views:
    781
    Steven T. Hatton
    Jul 10, 2005
  3. Tapeesh
    Replies:
    8
    Views:
    473
    alpha_beta_release
    Jan 13, 2006
  4. Rahul
    Replies:
    3
    Views:
    524
    Salt_Peter
    Jan 8, 2008
  5. Hicham Mouline
    Replies:
    0
    Views:
    632
    Hicham Mouline
    Apr 23, 2009
  6. Hicham Mouline
    Replies:
    1
    Views:
    580
    Michael DOUBEZ
    Apr 24, 2009
  7. somenath
    Replies:
    10
    Views:
    511
    James Kanze
    Jul 2, 2013
  8. somenath
    Replies:
    2
    Views:
    344
    somenath
    Aug 29, 2013
Loading...