C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Half-public Half-private member functions

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Y.H
Guest





PostPosted: Mon Dec 19, 2005 10:28 am    Post subject: Half-public Half-private member functions Reply with quote



Hi there,

Let's say we have 3 unrelated classes: A, B & C.
I was wondering about this design issue: is there any way/technique of
making a member function of class A visible to (= public, callable from)
class B but
invisible ( = private, incallable from) class C?
I wouldn't want to use 'friend' because it completely violates
encapsulation by giving B access to the private parts of A.

Thanks,
Yaron



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Nick C
Guest





PostPosted: Mon Dec 19, 2005 12:51 pm    Post subject: Re: Half-public Half-private member functions Reply with quote




"Y.H" <yhirsch (AT) netvision (DOT) net.il> wrote

Quote:
Hi there,

Let's say we have 3 unrelated classes: A, B & C.
I was wondering about this design issue: is there any way/technique of
making a member function of class A visible to (= public, callable from)
class B but
invisible ( = private, incallable from) class C?
I wouldn't want to use 'friend' because it completely violates
encapsulation by giving B access to the private parts of A.

Hi Yaron,

I guess I would ask the question: Is these classes are really unrelated,
then why do any of them need to call methods in the other? You are right to
avoid 'friend' though, as the need for these can often actually be a need to
refactor. In this case (given that I don't know anything about the actual
classes), I would give B an A (or a reference to one). If this would expose
too much of A to B, then I would think about refactoring/reworking the
design.

Alternatively, if it is just one A::method that B needs, and the design is
otherwise good, then you could use an interface: Create an abstract class
with your pure virtual method in it. Implement this method in A, then pass B
a reference to this base-class interface. Hey - presto! B gets access to
just the one function in A. There is no doubt a facy name for this pattern,
but I don't read as much as I should :-)

Nick C



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Maxim Yegorushkin
Guest





PostPosted: Mon Dec 19, 2005 12:52 pm    Post subject: Re: Half-public Half-private member functions Reply with quote




Y.H wrote:
Quote:
Hi there,

Let's say we have 3 unrelated classes: A, B & C.
I was wondering about this design issue: is there any way/technique of
making a member function of class A visible to (= public, callable from)
class B but
invisible ( = private, incallable from) class C?
I wouldn't want to use 'friend' because it completely violates
encapsulation by giving B access to the private parts of A.

You can do friend another way. Consider the following code, where only
B can have access to A::bar().

class A;

class B
{
private:
friend class A;
struct key {};
public:
void foo(A*);
};

class A
{
public:
void bar(B::key);
};

void B::foo(A* a) { a->bar(key()); }

int main()
{
A a;
B b;
b.foo(&a);
}


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Marc Girod
Guest





PostPosted: Mon Dec 19, 2005 12:53 pm    Post subject: Re: Half-public Half-private member functions Reply with quote

Quote:
"YH" == Y H <yhirsch (AT) netvision (DOT) net.il

YH> I wouldn't want to use 'friend' because it completely violates
YH> encapsulation by giving B access to the private parts of A.

It's yet the exact intention of 'friend'.
What do you mean byy 'completely'? Completely ...with respect to one
class, but it is your responsibility to design your classes!

So, split your A into sub-parts you want to expose and to hide, and
grant 'friendship' from one of them only.

--
Marc Girod P.O. Box 370 Voice: +358-71 80 25581
Nokia BI 00045 NOKIA Group Mobile: +358-50 38 78415
Kara RD B 480.2 Finland Fax: +358-71 80 64474

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Dervish
Guest





PostPosted: Mon Dec 19, 2005 1:48 pm    Post subject: Re: Half-public Half-private member functions Reply with quote

There are numerous ways to do this. Simplest way is to extract required
hidden function to separate class and use friend clause in this thin
class. E.g.:
class InterfaceA
{
protected:
virtual void foo() = 0;
friend class B;
};

class A : public InterfaceA
{
protected:
virtual void foo(){
cout << "class A foo" << endl;
}
};

class B {
public:
B()
{
InterfaceA *pA = new A();
pA->foo();
}
};


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Francis Glassborow
Guest





PostPosted: Mon Dec 19, 2005 2:44 pm    Post subject: Re: Half-public Half-private member functions Reply with quote

In article <do4ove$qiu$1 (AT) news2 (DOT) netvision.net.il>, Y.H
<yhirsch (AT) netvision (DOT) net.il> writes
Quote:
Hi there,

Let's say we have 3 unrelated classes: A, B & C.
I was wondering about this design issue: is there any way/technique of
making a member function of class A visible to (= public, callable from)
class B but
invisible ( = private, incallable from) class C?
I wouldn't want to use 'friend' because it completely violates
encapsulation by giving B access to the private parts of A.

<Unchecked>
If there is a need to do this (Surely very unusual) you need a nested
class of A that declares B a friend and provides a forwarding function
to the private function(s) you want to make available to B.
</unchecked>

However once you provide B any access to a private member of A you have
broken encapsulation and lost control of your private interface.

--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Michiel.Salters@tomtom.co
Guest





PostPosted: Mon Dec 19, 2005 2:45 pm    Post subject: Re: Half-public Half-private member functions Reply with quote


Y.H wrote:
Quote:
Hi there,

Let's say we have 3 unrelated classes: A, B & C.
I was wondering about this design issue: is there any way/technique of
making a member function of class A visible to (= public, callable from)
class B but
invisible ( = private, incallable from) class C?
I wouldn't want to use 'friend' because it completely violates
encapsulation by giving B access to the private parts of A.

Let A pass a pointer-to-member function to B. This gives B no more
access
than granted by A. You cannot prevent B from giving out that pointer,
of course
but the same holds for any friend: they can always implement a one-line
wrapper.

HTH,
Michiel Salters


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Y.H
Guest





PostPosted: Mon Dec 19, 2005 4:13 pm    Post subject: Re: Half-public Half-private member functions Reply with quote

Hi there,

By "unrelated" I meant that there's no inheritance relation between any of
them.

I thought about creating a base class with a pure virtual method, however
how would that

prevent C from accessing that method?

Yaron

"Nick C" <nick (AT) google (DOT) com> wrote

Quote:

"Y.H" <yhirsch (AT) netvision (DOT) net.il> wrote in message
news:do4ove$qiu$1 (AT) news2 (DOT) netvision.net.il...
Hi there,

Let's say we have 3 unrelated classes: A, B & C.
I was wondering about this design issue: is there any way/technique of
making a member function of class A visible to (= public, callable from)
class B but
invisible ( = private, incallable from) class C?
I wouldn't want to use 'friend' because it completely violates
encapsulation by giving B access to the private parts of A.

Hi Yaron,

I guess I would ask the question: Is these classes are really unrelated,
then why do any of them need to call methods in the other? You are right
to
avoid 'friend' though, as the need for these can often actually be a need
to
refactor. In this case (given that I don't know anything about the actual
classes), I would give B an A (or a reference to one). If this would
expose
too much of A to B, then I would think about refactoring/reworking the
design.

Alternatively, if it is just one A::method that B needs, and the design is
otherwise good, then you could use an interface: Create an abstract class
with your pure virtual method in it. Implement this method in A, then pass
B
a reference to this base-class interface. Hey - presto! B gets access to
just the one function in A. There is no doubt a facy name for this
pattern,
but I don't read as much as I should :-)

Nick C

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Marc Girod
Guest





PostPosted: Tue Dec 20, 2005 11:13 am    Post subject: Re: Half-public Half-private member functions Reply with quote

Quote:
"YH" == Y H <yhirsch (AT) netvision (DOT) net.il

YH> I thought about creating a base class with a pure virtual method,
YH> however how would that prevent C from accessing that method?

Private/friend.

Then 'completely' would mean exactly what you wanted and nothing more,
so that there would not be any 'violation' anymore.

Do I miss something?

--
Marc Girod P.O. Box 370 Voice: +358-71 80 25581
Nokia BI 00045 NOKIA Group Mobile: +358-50 38 78415
Kara RD B 480.2 Finland Fax: +358-71 80 64474

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Gerhard Menzl
Guest





PostPosted: Tue Dec 20, 2005 11:14 am    Post subject: Re: Half-public Half-private member functions Reply with quote

Nick C wrote:

Quote:
Alternatively, if it is just one A::method that B needs, and the
design is otherwise good, then you could use an interface: Create an
abstract class with your pure virtual method in it. Implement this
method in A, then pass B a reference to this base-class interface. Hey
- presto! B gets access to just the one function in A. There is no
doubt a facy name for this pattern,but I don't read as much as I
should Smile

It's called the Private Interface pattern. See

http://www.objectmentor.com/resources/articles/privateInterface.pdf

for details.

--
Gerhard Menzl

#dogma int main ()

Humans may reply by replacing the thermal post part of my e-mail address
with "kapsch" and the top level domain part with "net".

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.