Friends to select(ed) members? (Just checking)

Discussion in 'C++' started by Olumide, Feb 3, 2008.

  1. Olumide

    Olumide Guest

    Hello -

    I know friends normally gives a named class or function (hitherto
    called a guest) access to *all* (private and protected) members of the
    class that makes the declaration (hitherto called a host). ... I
    wonder if there's a way to limit the direct access of the guest class
    or function to a subset of the host's member function. For example

    class guest{

    };


    class host{
    private:
    friend class guest int number; // friendship restricted
    char* name;
    char sex;
    };

    Thank you for not flaming me :)

    - Olumide
    Olumide, Feb 3, 2008
    #1
    1. Advertising

  2. Olumide

    Pete Becker Guest

    On 2008-02-03 14:19:44 -0500, Olumide <> said:

    > I
    > wonder if there's a way to limit the direct access of the guest class
    > or function to a subset of the host's member function.
    >


    No.

    --
    Pete
    Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
    Standard C++ Library Extensions: a Tutorial and Reference
    (www.petebecker.com/tr1book)
    Pete Becker, Feb 3, 2008
    #2
    1. Advertising

  3. Olumide

    Jeff Schwab Guest

    Olumide wrote:
    > Hello -
    >
    > I know friends normally gives a named class or function (hitherto
    > called a guest) access to *all* (private and protected) members of the
    > class that makes the declaration (hitherto called a host). ... I
    > wonder if there's a way to limit the direct access of the guest class
    > or function to a subset of the host's member function. For example


    s/hitherto/hence forth/g

    > class guest{
    >
    > };
    >
    >
    > class host{
    > private:
    > friend class guest int number; // friendship restricted
    > char* name;
    > char sex;
    > };


    No. For the record, it sounds like "number" may not belong in host. It
    may be worth breaking the number sub-object out of host, and letting
    both host and guest refer to it. If you're sure that's not the right
    approach, then figure out why you think guest needs to be messing with
    host's internals.
    Jeff Schwab, Feb 3, 2008
    #3
  4. * Olumide:
    >
    > I know friends normally gives a named class or function (hitherto
    > called a guest) access to *all* (private and protected) members of the
    > class that makes the declaration (hitherto called a host). ... I
    > wonder if there's a way to limit the direct access of the guest class
    > or function to a subset of the host's member function. For example
    >
    > class guest{
    >
    > };
    >
    >
    > class host{
    > private:
    > friend class guest int number; // friendship restricted
    > char* name;
    > char sex;
    > };
    >
    > Thank you for not flaming me :)


    No direct support for that in language.

    You could do some contorted thing like

    #include <string>

    class Host;

    class Guest { void foo( Host& ); };

    class NumberHolder
    {
    friend class Guest;
    protected:
    int myNumber;
    NumberHolder( int v = 0 ): myNumber( v ) {}
    };

    enum GenderEnum { male, female, androgynous };

    class Host: public NumberHolder
    {
    private:
    std::string myName;
    GenderEnum myGender;
    };

    void Guest::foo( Host& h )
    {
    h.myNumber = 666;
    }

    int main()
    {}

    Actually I was surprised that this compiled without requiring accessing
    the h argument via a reference to NumberHolder type, but, enjoy.

    Or -- you really shouldn't be doing this!

    Think about whether that "partial friendship" doesn't imply a missing
    abstraction in the design, e.g. a class, or some other design issue.


    Cheers, & hth.,

    - Alf


    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Feb 3, 2008
    #4
  5. Olumide

    Olumide Guest

    On 3 Feb, 19:42, "Alf P. Steinbach" <> wrote:
    > No direct support for that in language.
    >
    > You could do some contorted thing like
    > ...
    > class NumberHolder
    > {
    > friend class Guest;
    > protected:
    > int myNumber;
    > NumberHolder( int v = 0 ): myNumber( v ) {}
    > };
    >
    > enum GenderEnum { male, female, androgynous };
    >
    > class Host: public NumberHolder
    > {
    > private:
    > std::string myName;
    > GenderEnum myGender;
    > };


    Thanks :) .
    Olumide, Feb 3, 2008
    #5
  6. Olumide

    Olumide Guest

    On 3 Feb, 19:50, Olumide <> wrote:
    > On 3 Feb, 19:42, "Alf P. Steinbach" <> wrote:
    >
    >
    >
    > > No direct support for that in language.

    >
    > > You could do some contorted thing like
    > > ...
    > > class NumberHolder
    > > {
    > > friend class Guest;
    > > protected:
    > > int myNumber;
    > > NumberHolder( int v = 0 ): myNumber( v ) {}
    > > };

    >
    > > enum GenderEnum { male, female, androgynous };

    >
    > > class Host: public NumberHolder
    > > {
    > > private:
    > > std::string myName;
    > > GenderEnum myGender;
    > > };

    >
    > Thanks :) .


    Inspired by your trickery, I just successfully tried

    class Guest{ };

    class Host{

    class SharedData{
    friend class Guest;
    int banana;
    };

    };


    .... objective secured! :-D
    Olumide, Feb 3, 2008
    #6
    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. fabio de francesco
    Replies:
    3
    Views:
    689
    fabio de francesco
    Jun 23, 2004
  2. Replies:
    4
    Views:
    389
    Noah Roberts
    Feb 14, 2005
  3. Replies:
    2
    Views:
    597
  4. Replies:
    0
    Views:
    656
  5. Brian Candler
    Replies:
    11
    Views:
    167
    Brian Candler
    Nov 3, 2008
Loading...

Share This Page