On Feb 21, 3:41*pm, Oliver <fwa...@gmail.com> wrote:
> Thanks for all your replies. Using Bar's member initialization list to initialize Foo will work in this case. *So I guess what follows is, if Foo's init depends on Bar, and Bar's init depends on Foo, that should be considered a bad design ...
>
> I wonder how to handle this particular design scenario I am dealing with:
>
> class Foo maintain a open file handle; (a piece of shared info)
>
> class Bar (or any number of other classes) contribute to write part of that file pending on the input.
Does each instance of Bar and other classes operates on one Foo, or
each operates on it's own Foo? If former, then you can pass Foo by
reference to all these instances and take care of the lifetime (Foo
must outlive all instances who hold it by reference). If later, then
Bar and others can also be constructed with a file name and create a
Foo on a per-instance basis.
(In any case, I would err towards former solution, see below).
>
> Class Foo co-ordinate the write and call Bar to do the actual work.
>
> In my current design, the class Foo has a member of Bar, but Bar needs tohave the file handler that Foo has, that is why I am thinking of passing that in through constructor ... maybe this is not right way. Can someone enlighten me?
In my mind, that's too complicated. In what way does Foo co-ordinate
the write? If it's merely "hold the file handle, provide a stream
(handle) to write on", then I would split it in two classes: base that
provides write operations, derived that holds the handle to write on.
Bar would only know of the base class (and hold a reference to it). In
fact, that's what all stream (file) libraries I know do, I am saying
nothing interesting here.
Goran.
|