Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #14370 > unrolled thread
| Started by | Oliver <fwang2@gmail.com> |
|---|---|
| First post | 2012-02-21 05:26 -0800 |
| Last post | 2012-02-21 23:44 -0800 |
| Articles | 9 — 7 participants |
Back to article view | Back to comp.lang.c++
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
| From | Oliver <fwang2@gmail.com> |
|---|---|
| Date | 2012-02-21 05:26 -0800 |
| Subject | question on no-argument constructor |
| Message-ID | <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
[toc] | [next] | [standalone]
| From | "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> |
|---|---|
| Date | 2012-02-21 14:32 +0100 |
| Message-ID | <ji06e9$tjp$1@dont-email.me> |
| In reply to | #14370 |
On 21.02.2012 14:26, Oliver wrote:
> 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 ...
Yes.
> at the risk of the object can exist without being properly initialized?
No.
> I must have missed something ...
Yes, but it's difficult to say what.
Cheers & hth.,
- Alf
[toc] | [prev] | [next] | [standalone]
| From | Nick Keighley <nick_keighley_nospam@hotmail.com> |
|---|---|
| Date | 2012-02-21 05:44 -0800 |
| Message-ID | <f174f82c-21bf-44f1-9945-3acdf1575285@gr6g2000vbb.googlegroups.com> |
| In reply to | #14371 |
Subject: question on no-argument constructor
aka "default constructor"
On Feb 21, 1:32 pm, "Alf P. Steinbach" <alf.p.steinbach
+use...@gmail.com> wrote:
> On 21.02.2012 14:26, Oliver wrote:
> > 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 ...
>
> Yes.
or some other constructor? Something must call Foo's constructor.
class Bar {
public:
Bar(): foo("hello")
{}
private:
Foo foo;
};
class Bar {
public:
Bar(const std::string& boom): foo(boom)
{}
private:
Foo foo;
};
> > at the risk of the object can exist without being properly initialized?
>
> No.
>
> > I must have missed something ...
>
> Yes, but it's difficult to say what.
[toc] | [prev] | [next] | [standalone]
| From | "Fred Zwarts \(KVI\)" <F.Zwarts@KVI.nl> |
|---|---|
| Date | 2012-02-21 14:44 +0100 |
| Message-ID | <ji074n$ppa$1@news.albasani.net> |
| In reply to | #14370 |
"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) {};
}
[toc] | [prev] | [next] | [standalone]
| From | Oliver <fwang2@gmail.com> |
|---|---|
| Date | 2012-02-21 06:41 -0800 |
| Message-ID | <2398288.12.1329835293884.JavaMail.geo-discussion-forums@vbck9> |
| In reply to | #14374 |
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) {};
> }
[toc] | [prev] | [next] | [standalone]
| From | Nick Keighley <nick_keighley_nospam@hotmail.com> |
|---|---|
| Date | 2012-02-21 08:14 -0800 |
| Message-ID | <7f38677c-48d4-4214-a660-0d404b1f10f4@s7g2000vby.googlegroups.com> |
| In reply to | #14379 |
please don't top post. if you are continuing the post you are replying
to the put your reply after it (or dispersed within it). remove
anything you aren't responding to. Often these deleteions are marked
<snip>. I've left your post unedited for top-posting.
On Feb 21, 2:41 pm, Oliver <fwa...@gmail.com> wrote:
class Bar {
public:
Bar(): foo("hello")
{}
private:
Foo foo;
}
> 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 ...
Foo doesn't really depend on Bar. Consider if foo were an int. When
int was designed (late Jurassic) no one knew of your Bar.
[<deity, I hate names like Foo and Bar...]
> 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 File holds a file handle of some sort (std::ifstream perhaps?)
> class Bar (or any number of other classes) contribute to write part of that file pending on the input.
various Writer classes use the File class to write to a file.
> Class Foo co-ordinate the write and call Bar to do the actual work.
oops. Lost me who is asking who to do the work? Could we scrap the
Foos and Bars and get some more concret names?
> 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?
I need a clearer requirement
> 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) {};
> > }- Hide quoted text -
>
> - Show quoted text -
[toc] | [prev] | [next] | [standalone]
| From | Juha Nieminen <nospam@thanks.invalid> |
|---|---|
| Date | 2012-02-21 17:00 +0000 |
| Message-ID | <4f43cdae$0$2819$7b1e8fa0@news.nbl.fi> |
| In reply to | #14382 |
Nick Keighley <nick_keighley_nospam@hotmail.com> wrote:
> class Bar {
> public:
> Bar(): foo("hello")
> {}
>
> private:
> Foo foo;
> }
In C++11 you can also do this:
class Bar
{
private:
Foo foo("hello");
};
(which effectively amounts to the same, but is less verbose.)
[toc] | [prev] | [next] | [standalone]
| From | Jorgen Grahn <grahn+nntp@snipabacken.se> |
|---|---|
| Date | 2012-02-21 21:12 +0000 |
| Message-ID | <slrnjk8257.1ls.grahn+nntp@frailea.sa.invalid> |
| In reply to | #14379 |
On Tue, 2012-02-21, Oliver wrote: > > Thanks for all your replies. Using Bar's member initialization list ... I was going to reply with something helpful here, but you've topposted (mentioned by someone else) and also not broken your lines - your posting was hundreds of characters wide. That last thing in particular is a showstopper for me. /Jorgen -- // Jorgen Grahn <grahn@ Oo o. . . \X/ snipabacken.se> O o .
[toc] | [prev] | [next] | [standalone]
| From | Goran <goran.pusic@gmail.com> |
|---|---|
| Date | 2012-02-21 23:44 -0800 |
| Message-ID | <23f4ed80-684e-446b-b9d9-45e23483caaa@eq5g2000vbb.googlegroups.com> |
| In reply to | #14379 |
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 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? 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.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c++
csiph-web