Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #14379

Re: question on no-argument constructor

From Oliver <fwang2@gmail.com>
Newsgroups comp.lang.c++
Subject Re: question on no-argument constructor
Date 2012-02-21 06:41 -0800
Organization http://groups.google.com
Message-ID <2398288.12.1329835293884.JavaMail.geo-discussion-forums@vbck9> (permalink)
References <18415058.42.1329830819543.JavaMail.geo-discussion-forums@vbne13> <ji074n$ppa$1@news.albasani.net>

Show all headers | View raw


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.

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 to have 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?

Thanks

Oliver





 











On Tuesday, February 21, 2012 8:44:55 AM UTC-5, F.Zwarts wrote:
> "Oliver"  wrote in message 
> news:18415058.42.1329830819543.JavaMail.geo-discussion-forums@vbne13...
> >
> >hi, all -
> >
> >I have a newbie question: suppose I have a user-defined class that must 
> >take a argument to properly initialize. so I have something like:
> >
> >class Foo {
> >   explicit Foo(const string &str);
> >...
> >}
> >
> >Another class define Foo as its member:
> >
> >class Bar {
> >
> >private:
> >   Foo foo;
> >}
> >
> >Now the compiler complains that it can find the candidate constructor. So 
> >it seems I must define a no-argument constructor for Foo ... at the risk of 
> >the object can exist without being properly initialized? I must have missed 
> >something ...
> >
> >Thanks for your help
> >
> >Oliver
> 
> Initialize foo in the constructor of bar, e.g.:
> 
> class Bar {
> 
> private:
>    Foo foo;
> 
>    explicit Bar (const string &str) : foo (str) {};
> }

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

question on no-argument constructor Oliver <fwang2@gmail.com> - 2012-02-21 05:26 -0800
  Re: question on no-argument constructor "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2012-02-21 14:32 +0100
    Re: question on no-argument constructor Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-02-21 05:44 -0800
  Re: question on no-argument constructor "Fred Zwarts \(KVI\)" <F.Zwarts@KVI.nl> - 2012-02-21 14:44 +0100
    Re: question on no-argument constructor Oliver <fwang2@gmail.com> - 2012-02-21 06:41 -0800
      Re: question on no-argument constructor Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-02-21 08:14 -0800
        Re: question on no-argument constructor Juha Nieminen <nospam@thanks.invalid> - 2012-02-21 17:00 +0000
      Re: question on no-argument constructor Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-02-21 21:12 +0000
      Re: question on no-argument constructor Goran <goran.pusic@gmail.com> - 2012-02-21 23:44 -0800

csiph-web