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

O

Olumide

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
 
J

Jeff Schwab

Olumide said:
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.
 
A

Alf P. Steinbach

* 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
 
O

Olumide

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 :) .
 
O

Olumide


Inspired by your trickery, I just successfully tried

class Guest{ };

class Host{

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

};


.... objective secured! :-D
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
474,183
Messages
2,570,968
Members
47,518
Latest member
TobiasAxf

Latest Threads

Top