Is it OK to specify a _private_ member function as a friend?

Discussion in 'C++' started by Risto Lankinen, Dec 3, 2003.

  1. Hi!

    Should this compile:

    class C
    {
    private:
    void f();
    };

    class D
    {
    friend void C::f(); // <- "line 10"
    };

    int main()
    {
    return 0;
    }

    It compiles fine with many compilers, but one compiler rejects
    it with the following error:

    "test.cpp", line 10: Error: #265-D: function "C::f" is inaccessible

    The explanation provided by the error message borders with
    reasonable (since C::f after all is a private function, and hence
    should be inaddressable by anyone else). Now I'm in doubt
    whether this is a bug in said compiler, or if it is an exception
    in the access control mechanism. I'm leaning towards a bug
    in this compiler since so many others accept it, but then again,
    a million flies can't be wrong... :)

    What does the standard say, anyway?

    Cheers!

    - Risto -
     
    Risto Lankinen, Dec 3, 2003
    #1
    1. Advertising

  2. Risto Lankinen

    Dan W. Guest

    On Wed, 03 Dec 2003 11:29:19 GMT, "Risto Lankinen"
    <> wrote:

    >Hi!
    >
    >Should this compile:
    >
    >class C
    >{
    >private:
    > void f();
    >};
    >
    >class D
    >{
    > friend void C::f(); // <- "line 10"
    >};
    >
    >int main()
    >{
    > return 0;
    >}
    >
    >It compiles fine with many compilers, but one compiler rejects
    >it with the following error:
    >
    >"test.cpp", line 10: Error: #265-D: function "C::f" is inaccessible
    >
    >The explanation provided by the error message borders with
    >reasonable (since C::f after all is a private function, and hence
    >should be inaddressable by anyone else). Now I'm in doubt
    >whether this is a bug in said compiler, or if it is an exception
    >in the access control mechanism. I'm leaning towards a bug
    >in this compiler since so many others accept it, but then again,
    >a million flies can't be wrong... :)
    >
    >What does the standard say, anyway?
    >
    >Cheers!
    >
    > - Risto -
    >
    >

    When you declare anything "friend", it means that you're giving it
    permission to access you, NOT giving yourself permission to access it.
    If you want class D to access a private function of class C, class C
    must declare class D friend, or, preferably, just the function of
    class D that needs to call C::f().
     
    Dan W., Dec 3, 2003
    #2
    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. Replies:
    3
    Views:
    362
    Rolf Magnus
    Apr 5, 2006
  2. Replies:
    1
    Views:
    1,338
    Jim Langston
    Nov 9, 2006
  3. Replies:
    2
    Views:
    672
    Triple-DES
    Feb 26, 2008
  4. Matthew
    Replies:
    3
    Views:
    359
    Matthew
    Feb 8, 2009
  5. Peter
    Replies:
    2
    Views:
    276
    Öö Tiib
    Jun 6, 2013
Loading...

Share This Page