| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"Olumide" |
| Date: |
03 Feb 2008 01:19:44 PM |
| Object: |
Friends to select(ed) members? (Just checking) |
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
.
|
|
| User: "Alf P. Steinbach" |
|
| Title: Re: Friends to select(ed) members? (Just checking) |
03 Feb 2008 01:42:57 PM |
|
|
* 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?
.
|
|
|
| User: "Olumide" |
|
| Title: Re: Friends to select(ed) members? (Just checking) |
03 Feb 2008 01:50:25 PM |
|
|
On 3 Feb, 19:42, "Alf P. Steinbach" <al...@start.no> 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 :-) .
.
|
|
|
| User: "Olumide" |
|
| Title: Re: Friends to select(ed) members? (Just checking) |
03 Feb 2008 01:56:14 PM |
|
|
On 3 Feb, 19:50, Olumide <50...@web.de> wrote:
On 3 Feb, 19:42, "Alf P. Steinbach" <al...@start.no> 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
.
|
|
|
|
|
|
| User: "Jeff Schwab" |
|
| Title: Re: Friends to select(ed) members? (Just checking) |
03 Feb 2008 01:27:50 PM |
|
|
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.
.
|
|
|
|
| User: "Pete Becker" |
|
| Title: Re: Friends to select(ed) members? (Just checking) |
03 Feb 2008 01:21:06 PM |
|
|
On 2008-02-03 14:19:44 -0500, Olumide <50295@web.de> 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)
.
|
|
|
|

|
Related Articles |
|
|