Restricted friendship

D

Dave

Hello all,

I need an idiom to do the following:

I have a set of non-public member functions in my class. I would like to
grant access to *some* of them to another class. I can't just declare the
other class a friend because that would grant access to *all* member
functions.

Is there any way I can accomplish this?

Thanks,
Dave
 
E

E. Robert Tisdale

Dave said:
I need an idiom to do the following:

I have a set of non-public member functions in my class.
I would like to grant access to *some* of them to another class.
I can't just declare the other class a friend
because that would grant access to *all* member functions.

Is there any way I can accomplish this?


class X {
public:
class Y {
private:
// other private data members
};
private:
// some private data members
Y y;
public:
friend
class Z;
};

class Z has access to some private data members but not others.
 
V

Victor Bazarov

Dave said:
I need an idiom to do the following:

I have a set of non-public member functions in my class. I would like to
grant access to *some* of them to another class. I can't just declare the
other class a friend because that would grant access to *all* member
functions.

Is there any way I can accomplish this?

Extract those functions in a base class and grant friendship there.

Why do you need this, BTW?

V
 
E

Efrat Regev

Dave said:
Hello all,

I need an idiom to do the following:

I have a set of non-public member functions in my class. I would like to
grant access to *some* of them to another class. I can't just declare the
other class a friend because that would grant access to *all* member
functions.

Is there any way I can accomplish this?

Thanks,
Dave


There might be a technical answer to your problem, but I wonder if your
problem doesn't indicate room for design improvement (of course, maybe not).
Specifically, since you identify a method subgroup with some common
properties - the need to grant friendship, it's likely they share other
common properties as well. Perhaps these methods deserve a class of their
own (in which, btw, you could grant access to their friend).
 

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

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top