Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Method that only one other class type may access

Reply
Thread Tools

Method that only one other class type may access

 
 
Christopher
Guest
Posts: n/a
 
      10-01-2011
I know I need some sort of friend, but I don't want to friend the
entire class.

I've got a TcpConnection class and it has a state member.
I've got a TcpListener class that accepts connections and creates a
TcpConnectionClass.

The TcpListener class needs to be able to change the state member of
the TcpConnection class, but I don't want anyone else to be able to.
How do I go about that?

 
Reply With Quote
 
 
 
 
Christopher
Guest
Posts: n/a
 
      10-01-2011
On Oct 1, 3:32*pm, Christopher <(E-Mail Removed)> wrote:
> I know I need some sort of friend, but I don't want to friend the
> entire class.
>
> I've got a TcpConnection class and it has a state member.
> I've got a TcpListener class that accepts connections and creates a
> TcpConnectionClass.
>
> The TcpListener class needs to be able to change the state member of
> the TcpConnection class, but I don't want anyone else to be able to.
> How do I go about that?


Problem 1 is that the first class needs to be aware of the second, so
when I use friend function I have a circular dependency problem when
keeping seperate headers.

Problem 2 is that the function I want to friend is virtual. I cannot
find whether or not making it a friend also makes its derived type
implementations of that function a friend also.

Problem 3 is that I will also derive from the class I am allowing
access to.
So, I want class B and all its derived types to have access
to the data in class A and all its derived types.
 
Reply With Quote
 
 
 
 
Marcel Müller
Guest
Posts: n/a
 
      10-01-2011
On 01.10.2011 22:32, Christopher wrote:
> I know I need some sort of friend, but I don't want to friend the
> entire class.
>
> I've got a TcpConnection class and it has a state member.
> I've got a TcpListener class that accepts connections and creates a
> TcpConnectionClass.
>
> The TcpListener class needs to be able to change the state member of
> the TcpConnection class, but I don't want anyone else to be able to.
> How do I go about that?


Most likely you have a design problem. A listener should not change any
other state than its own. A listener is a listener and a connection is a
connection. Why should a listener manipulate the connection state. What
about race conditions?

There are ways to do so, but I am in doubt whether this is a good
advise. You could create a base class of your connection to isolate the
state member and make the listener a friend of this base class.


Marcel
 
Reply With Quote
 
Christopher
Guest
Posts: n/a
 
      10-01-2011
On Oct 1, 4:38*pm, Marcel Müller <(E-Mail Removed)> wrote:
> On 01.10.2011 22:32, Christopher wrote:
>
> > I know I need some sort of friend, but I don't want to friend the
> > entire class.

>
> > I've got a TcpConnection class and it has a state member.
> > I've got a TcpListener class that accepts connections and creates a
> > TcpConnectionClass.

>
> > The TcpListener class needs to be able to change the state member of
> > the TcpConnection class, but I don't want anyone else to be able to.
> > How do I go about that?

>
> Most likely you have a design problem. A listener should not change any
> other state than its own. A listener is a listener and a connection is a
> connection. Why should a listener manipulate the connection state. What
> about race conditions?
>
> There are ways to do so, but I am in doubt whether this is a good
> advise. You could create a base class of your connection to isolate the
> state member and make the listener a friend of this base class.
>
> Marcel


Well, the listener accepts connections right?
So, it creates a connection in the NOT_CONNECTED state.
It queues it up for an accept via boost::asio
When the accept completes the listener is notified via boost::asio
the listener has to be notified, so it can add the connection to its
collection of clients
Now we have a connected connection in a NOT_CONNECTED state = bad.

I was fiddling with the boost::binds that the boost::asio example uses
and was thinking maybe:
I could have the listener post the accept request with a callback to
the connection itself
Have the connection itself, in its callback, call a callback on the
listener.

I don't want to turn things in to spaghetti though.

 
Reply With Quote
 
Marcel Müller
Guest
Posts: n/a
 
      10-02-2011
On 02.10.2011 00:34, Christopher wrote:
>> Most likely you have a design problem. A listener should not change any
>> other state than its own. A listener is a listener and a connection is a
>> connection. Why should a listener manipulate the connection state. What
>> about race conditions?
>>
>> There are ways to do so, but I am in doubt whether this is a good
>> advise. You could create a base class of your connection to isolate the
>> state member and make the listener a friend of this base class.

>
> Well, the listener accepts connections right?
> So, it creates a connection in the NOT_CONNECTED state.
> It queues it up for an accept via boost::asio
> When the accept completes the listener is notified via boost::asio
> the listener has to be notified, so it can add the connection to its
> collection of clients
> Now we have a connected connection in a NOT_CONNECTED state = bad.


Move the connection pool to the connection class (or a separate
connection pool class) and provide a public factory function that
initiates a connected connection.


> I was fiddling with the boost::binds that the boost::asio example uses
> and was thinking maybe:
> I could have the listener post the accept request with a callback to
> the connection itself
> Have the connection itself, in its callback, call a callback on the
> listener.


Sorry, I didn't catch what you mean.


Marcel
 
Reply With Quote
 
Noah Roberts
Guest
Posts: n/a
 
      10-02-2011
On Oct 1, 1:32*pm, Christopher <(E-Mail Removed)> wrote:
> I know I need some sort of friend, but I don't want to friend the
> entire class.
>
> I've got a TcpConnection class and it has a state member.
> I've got a TcpListener class that accepts connections and creates a
> TcpConnectionClass.
>
> The TcpListener class needs to be able to change the state member of
> the TcpConnection class, but I don't want anyone else to be able to.
> How do I go about that?


I think you could do this with a function object. Something like this
untested bit:

struct TcpListener;

struct TcpConnection
{
struct funky_operator
{
friend class TcpListener;
funky_operator(TcpConnection * c) : conn(c) {}
private:
void operator() (??) { ?? }
TcpConnection * conn;
};
friend class funky_operator;
funky_operator op;

TcpConnection() : op(this) {}
};

Now, everyone can see "op" but only TcpListener can call it.
 
Reply With Quote
 
Noah Roberts
Guest
Posts: n/a
 
      10-02-2011
On Oct 1, 1:50*pm, Christopher <(E-Mail Removed)> wrote:
> On Oct 1, 3:32*pm, Christopher <(E-Mail Removed)> wrote:
>
> > I know I need some sort of friend, but I don't want to friend the
> > entire class.

>
> > I've got a TcpConnection class and it has a state member.
> > I've got a TcpListener class that accepts connections and creates a
> > TcpConnectionClass.

>
> > The TcpListener class needs to be able to change the state member of
> > the TcpConnection class, but I don't want anyone else to be able to.
> > How do I go about that?

>
> Problem 1 is that the first class needs to be aware of the second, so
> when I use friend function I have a circular dependency problem when
> keeping seperate headers.


No you don't, because you're going to forward declare your classes
only in headers and only include stuff you are USING in that file.
>
> Problem 2 is that the function I want to friend is virtual. I cannot
> find whether or not making it a friend also makes its derived type
> implementations of that function a friend also.


Have the functor I described in my last post call a protected virtual
function.
>
> Problem 3 is that I will also derive from the class I am allowing
> access to.
> * * * * * So, I want class B and all its derived types to have access
> to the data in class A and all its derived types.


Have a protected function in the base Listener that can be called by
derived functions. Do your friendly access here. Your derived
objects won't be able to access the class that friended the base
directly.

Of course, as others have said this is probably a bad design. It is
certainly more than just a little coupled. But if you really need to,
that's one way it could be done.
 
Reply With Quote
 
Paul N
Guest
Posts: n/a
 
      10-03-2011
On Oct 1, 9:32*pm, Christopher <(E-Mail Removed)> wrote:
> I know I need some sort of friend, but I don't want to friend the
> entire class.
>
> I've got a TcpConnection class and it has a state member.
> I've got a TcpListener class that accepts connections and creates a
> TcpConnectionClass.
>
> The TcpListener class needs to be able to change the state member of
> the TcpConnection class, but I don't want anyone else to be able to.
> How do I go about that?


As a very low tech solution - but which may be sufficient for you...

Why not provide a suitable access function to do what needs to be
done, but give it a long name that includes the name of the class/
function that you want to give access to? Any other programmers will
see that the function exists but will realise that they're not meant
to use it.

Paul.
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      10-03-2011
On 10/ 2/11 09:32 AM, Christopher wrote:
> I know I need some sort of friend, but I don't want to friend the
> entire class.
>
> I've got a TcpConnection class and it has a state member.
> I've got a TcpListener class that accepts connections and creates a
> TcpConnectionClass.
>
> The TcpListener class needs to be able to change the state member of
> the TcpConnection class, but I don't want anyone else to be able to.
> How do I go about that?


If a design doesn't fit the problem, the design is flawed. It looks to
me like you are over engineering what is a very simple (and common)
task. Don't fret though, I see this all the time! Socket related
activities simply don't fit well within an OO approach.

--
Ian Collins
 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      10-03-2011
On Mon, 2011-10-03, Ian Collins wrote:
> On 10/ 2/11 09:32 AM, Christopher wrote:
>> I know I need some sort of friend, but I don't want to friend the
>> entire class.
>>
>> I've got a TcpConnection class and it has a state member.
>> I've got a TcpListener class that accepts connections and creates a
>> TcpConnectionClass.
>>
>> The TcpListener class needs to be able to change the state member of
>> the TcpConnection class, but I don't want anyone else to be able to.
>> How do I go about that?

>
> If a design doesn't fit the problem, the design is flawed. It looks to
> me like you are over engineering what is a very simple (and common)
> task. Don't fret though, I see this all the time! Socket related
> activities simply don't fit well within an OO approach.


It is not clear to me what you mean with that last sentence, but I was
going to say something similar:

I see no great benefits of wrapping the BSD socket API in lots of stuff.
If you wrap them a bit to get:

- file descriptors which aren't convertible to and from ints
- RAII

then the API is pretty usable as it is. Bonus: anyone who knows the
API doesn't have to relearn another one.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
why a class can't access protected method from another class in thesame package,the method is interited from the ohtner class from differntpackage? junzhang1983@gmail.com Java 3 01-28-2008 02:09 AM
Error - Only one WebServiceBinding attribute may be specified on type Pankaj Garg ASP .Net Web Services 1 02-24-2005 05:39 AM
Re: How do I access another type's method from one type's method Howard C++ 2 07-04-2003 12:08 PM
Re: How do I access another type's method from one type's method Rolf Magnus C++ 1 07-04-2003 02:38 AM



Advertisments