Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > question on no-argument constructor

Reply
Thread Tools

question on no-argument constructor

 
 
Oliver
Guest
Posts: n/a
 
      02-21-2012
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
 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      02-21-2012
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
 
Reply With Quote
 
 
 
 
Nick Keighley
Guest
Posts: n/a
 
      02-21-2012
Subject: question on no-argument constructor

aka "default constructor"

On Feb 21, 1:32*pm, "Alf P. Steinbach" <alf.p.steinbach
(E-Mail Removed)> 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.

 
Reply With Quote
 
Fred Zwarts \(KVI\)
Guest
Posts: n/a
 
      02-21-2012
"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) {};
}

 
Reply With Quote
 
Oliver
Guest
Posts: n/a
 
      02-21-2012

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) {};
> }


 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      02-21-2012
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 <(E-Mail Removed)> 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 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?


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 -


 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      02-21-2012
Nick Keighley <(E-Mail Removed)> 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.)
 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      02-21-2012
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 .
 
Reply With Quote
 
Goran
Guest
Posts: n/a
 
      02-22-2012
On Feb 21, 3:41*pm, Oliver <(E-Mail Removed)> 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.
 
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
A constructor calling another constructor (default constructor)? Generic Usenet Account C++ 10 11-28-2007 04:12 AM
Constructor question, how does the call to the parent constructor work? marcwentink@hotmail.com C++ 6 05-09-2006 07:19 AM
java like constructor calling constructor lallous C++ 5 01-23-2004 11:52 PM
calling a constructor within a constructor Brett Irving C++ 3 06-29-2003 10:43 AM
why it's not possible calling constructor from constructor? Giulio C++ 9 06-25-2003 03:56 PM



Advertisments