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


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

Re: question on no-argument constructor

From "Fred Zwarts \(KVI\)" <F.Zwarts@KVI.nl>
Newsgroups comp.lang.c++
Subject Re: question on no-argument constructor
Date 2012-02-21 14:44 +0100
Organization albasani.net
Message-ID <ji074n$ppa$1@news.albasani.net> (permalink)
References <18415058.42.1329830819543.JavaMail.geo-discussion-forums@vbne13>

Show all headers | View raw


"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