delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2002/01/14/02:32:43

X-Authentication-Warning: delorie.com: mailnull set sender to djgpp-bounces using -f
From: "Tim Van Holder" <notme AT spamproofemail DOT com>
Subject: Re: BUG? : Constructing member objects on-the-fly inside constructors.
Date: Mon, 14 Jan 2002 08:25:09 +0100
Newsgroups: comp.os.msdos.djgpp
Organization: Anubex N.V.
Message-ID: <pan.2002.01.14.08.25.09.366.449@spamproofemail.com>
References: <a1mf88$4b3$1 AT knossos DOT btinternet DOT com>
User-Agent: Pan/0.10.0 (Unix)
Mime-Version: 1.0
X-Comment-To: "Robert Hardy" <maprph AT bath DOT ac DOT uk>
X-No-Productlinks: Yes
Lines: 80
NNTP-Posting-Host: 194.78.64.238
X-Trace: 1010993323 reader0.news.skynet.be 75157 194.78.64.238
X-Complaints-To: abuse AT skynet DOT be
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

> class contains_int {
>       int i;
>       public:
>              contains_int(int ii): i(ii) {}
>              void printme() {cout << "( int: " << i << " )"; } int
>              getme() {return i;}
> };
> 
> class contains_contains_int {
>       contains_int c_i;
>       public:
>              contains_contains_int(contains_int c_ii): c_i(c_ii) {} void
>              printme() {cout << "( c_i : ";
>                   c_i.printme(); cout << " )"; }
> };
> 
> class contains_contains_contains_int {
>       contains_contains_int cc_i;
>       public:
>              contains_contains_contains_int(contains_contains_int cc_ii)
>              : cc_i(cc_ii) {}
>              void printme() {cout << "( cc_i : ";
>                   cc_i.printme(); cout << " )"; }
> };

Just one question: why not have them all take an int as constructor
argument and pass that down?

class cont_int {
  int _i;
  public:
    cont_int(int i) : _i(i) {}
  ...
};

class cont_cont_int {
  cont_int _ci;
  public:
    cont_cont_int(int i) : _ci(i) {}
  ...
};

class cont_cont_cont_int {
  int _cci;
  public:
    cont_cont_cont_int(int i) : _cci(i) {}
  ...
};

Of course, you can also define the constructors that take the actual
objects, depending on whether they're needed (and even then, I'd be
inclined to use their get() accessors). This may also solve the problem
below.

>     contains_contains_contains_int x( contains_contains_int (
>     contains_int(10) )); cout << " x = "; x.printme(); cout << endl;    
>         //line A
> 
> }
> 
> In function `int main()':
> Error (line A): request for member `printme' in `x', which is of
> non-aggregate type `contains_contains_contains_int ()()'

My guess is that the compile chose to interpret the declaration of x as a
function declaration (it's a known ambiguity in the language, though I
thought it was limited to templatized types).
Writing it as

  type name = type(args);

instead of

   type name(args);

would probably help, as would using a simpler constructor (such as
passing down the ints, as they are really what matters; how you contain
it is an implementation detail).

HTH

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019