Class in class OR class derived from class? 
Author Message
 Class in class OR class derived from class?

Hi,

If have a newbie problem with understanding classes.

I will give an example with CNntp and CSocket ; Should I do this :

    CNntp : private CSocket {
        // member variables
        ...
        // member ftns
        ...
    }
or this:
    CNntp {
        // member variables
    private:
        CSocket m_Socket;
        ...
        //  member functies
        ...
    }

Through an CNntp-object I don't need to access any CSocket functions
directly.

I can see offcourse that there will be a difference in the implementation
file.  I quess it would both work, but  I can't figure out which of the two
I should use.  Can anyone tell me which one to use and why ?

Thanks, many greetz, Bart.



Wed, 03 Mar 2004 22:03:01 GMT  
 Class in class OR class derived from class?

Quote:

> Hi,

> If have a newbie problem with understanding classes.

> I will give an example with CNntp and CSocket ; Should I do this :

>     CNntp : private CSocket {
>         // member variables
>         ...
>         // member ftns
>         ...
>     }
> or this:
>     CNntp {
>         // member variables
>     private:
>         CSocket m_Socket;
>         ...
>         //  member functies
>         ...
>     }

> Through an CNntp-object I don't need to access any CSocket functions
> directly.

> I can see offcourse that there will be a difference in the implementation
> file.  I quess it would both work, but  I can't figure out which of the two
> I should use.  Can anyone tell me which one to use and why ?

> Thanks, many greetz, Bart.

I would suggest designing it so that there could be multiple sockets,
whether you need that now or not.  It's just a thinking/partitioning
technique.  That means you would derive a class from CSocket like
CNntpSocket and put details common to all NNTP sockets in that class.
But then you would probably also need a class that contained a
CNntpSocket member, or a dynamic array of them, to manage how the
socket(s) interface with the application.

Another very practical reason to do it this way is that (in the opinion
of many) CSocket is a rather poor design, while CAsyncSocket is quite
useful and more flexible (but requires quite a bit more learning and
code).  To use CAsyncSocket it is mandatory to inherit from it.  So
starting with an inherited socket class would make potential design
changes easier in the future.

--
Scott McPhillips [VC++ MVP]



Wed, 03 Mar 2004 23:26:00 GMT  
 Class in class OR class derived from class?
Thank for replying :

Quote:
> I would suggest designing it so that there could be multiple sockets,
> whether you need that now or not.  It's just a thinking/partitioning
> technique.  That means you would derive a class from CSocket like
> CNntpSocket and put details common to all NNTP sockets in that class.
> But then you would probably also need a class that contained a
> CNntpSocket member, or a dynamic array of them, to manage how the
> socket(s) interface with the application.

Don't fully understand it yet, with multiple socket, do you mean :

1) the possibilty to use multiple sockets  in one CNntpSocket ?
2) the possibilty to use multiple nntp-sockets trough creating multiple
objects of the CNntpSocket-class ?
If you meant the second one, than that should work too if the CNntp is not
derived from CWinsock,
but contains a CSocket member, or not ?

With CAsyncSocket, I have no problem anymore, since you said it is mandatory
to inherit...
Can you give me some more info ?

Thank you, many greetz...
Bart



Thu, 04 Mar 2004 04:36:04 GMT  
 Class in class OR class derived from class?

Quote:

> Thank for replying :

> > I would suggest designing it so that there could be multiple sockets,
> > whether you need that now or not.  It's just a thinking/partitioning
> > technique.  That means you would derive a class from CSocket like
> > CNntpSocket and put details common to all NNTP sockets in that class.
> > But then you would probably also need a class that contained a
> > CNntpSocket member, or a dynamic array of them, to manage how the
> > socket(s) interface with the application.

> Don't fully understand it yet, with multiple socket, do you mean :

> 1) the possibilty to use multiple sockets  in one CNntpSocket ?
> 2) the possibilty to use multiple nntp-sockets trough creating multiple
> objects of the CNntpSocket-class ?
> If you meant the second one, than that should work too if the CNntp is not
> derived from CWinsock,
> but contains a CSocket member, or not ?

> With CAsyncSocket, I have no problem anymore, since you said it is mandatory
> to inherit...
> Can you give me some more info ?

> Thank you, many greetz...
> Bart

I meant (2).  Your question was about deriving vs. embedding. I
suggested you do both and embed a derived class. Planning for the
possibility of multiple nntp sockets helps clarify what goes where.

CSocket provides blocking calls - Receive and Send do not return until
they have finished the exact byte count requested.  This can be a
problem in the main GUI thread because it stops the program from
responding to user input while it is blocked in a socket call.
CAsyncSocket provides nonblocking calls - Receive and Send calls always
return quickly.  But, they don't always work and they may transfer more
or less data than you wish.

--
Scott McPhillips [VC++ MVP]



Thu, 04 Mar 2004 06:12:34 GMT  
 Class in class OR class derived from class?
  I think you should use "Class in class",because according you description
the CNntp class is a class that only use another class - CSocket and the
CNntp don't need dervie any members from the CSocket.

--
Zero Lu

Quote:

>Hi,

>If have a newbie problem with understanding classes.

>I will give an example with CNntp and CSocket ; Should I do this :



Thu, 04 Mar 2004 02:11:51 GMT  
 Class in class OR class derived from class?
Thank you for replying back,

Have a new question about CAsyncSocket,
when I use an object of it in the main thread, and then
perform create, it makes a Window 'socket notification' IN THE MAIN THREAD
and an extra thread...

My question is, assume that I constantly receive lots of data -> OnReceive
will be called constantly....
will this not make the mainthread block, because the 'socket notification'
window is in the mainthread ?

I know I should look for a guide or book, about CAsyncSocket and CSocket, no
recommandations ?

Thank you, greetz Bart



Fri, 05 Mar 2004 02:04:34 GMT  
 Class in class OR class derived from class?

Quote:

> Thank you for replying back,

> Have a new question about CAsyncSocket,
> when I use an object of it in the main thread, and then
> perform create, it makes a Window 'socket notification' IN THE MAIN THREAD
> and an extra thread...

> My question is, assume that I constantly receive lots of data -> OnReceive
> will be called constantly....
> will this not make the mainthread block, because the 'socket notification'
> window is in the mainthread ?

> I know I should look for a guide or book, about CAsyncSocket and CSocket, no
> recommandations ?

> Thank you, greetz Bart

Receiving lots of data in the main thread can slow down GUI
responsiveness but it does not block the GUI.  The OnReceive
notifications come in via the same message queue that provides keys,
mouse, etc.  Both kinds of messages are processed on a first-come,
first-served basis.  A GUI input will therefore not have to wait longer
than it takes to process one (or a very few) OnReceive messages.  Using
bigger buffers (taking bigger chunks of data from the socket) will
reduce the number of OnReceive messages.  Everything keeps running as
fast as it can go, which is much much better than blocking.

There is very little literature on CAsyncSocket.  There is a lot of
excellent literature on winsock, and CAsyncSocket is a fairly thin
wrapper around winsock.  Study winsock and you will understand
CAsyncSocket.  The Windows Sockets book by Quinn & Shute is one good
source.  Also see
http://www.{*filter*}port.com/~tangent/programming/winsock/

--
Scott McPhillips [VC++ MVP]



Fri, 05 Mar 2004 02:58:28 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Class Wizard won't let me derive a class from my class

2. New Class derived from Class which derived from CWindowImpl

3. Activating the class wizard on a derived-derived Cwnd class

4. How to Create CCtrlView derived class based on CTreeCtrl derived class

5. How to Create CCtrlView derived class based on CTreeCtrl derived class

6. Internal Compiler Error while deriving C# class from a MC++ class implementing a C# interface

7. How to Stop Derived Class from overriding base class method

8. creating an instance of a derived class from a base class

9. How to prevent derived classes from changing a base class function

10. Classview not showing classes derived from classes in user namespace

11. classes derived from classes in namespaces

12. address of base class and derived class different

 

 
Powered by phpBB® Forum Software