From 2733614181043890561
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,5ba1c16c1a513bca
X-Google-Attributes: gidf78e5,public
From: feng chen <fchen@fas.harvard.edu>
Subject: Re: valid code?
Date: 2000/03/21
Message-ID: <8b5sub$ps6$1@news.fas.harvard.edu>#1/1
X-Deja-AN: 600054474
X-Mail2News-Path: news.demon.net!mulga.cs.mu.oz.au
Approved: Fergus Henderson <fjh@cs.mu.oz.au>, moderator of comp.std.c++
References: <8atgsi$s0f$1@news.luth.se> <38D264E3.41E7620C@sensor.com>
X-Trace: mail2news.demon.co.uk 953580338 mail2news:16765 mail2news mail2news.demon.co.uk
Organization: Harvard University, Cambridge, Massachusetts
X-Robomod: STUMP, ichudov@algebra.com (Igor Chudov)
X-Authentication-Warning: mulga.cs.mu.OZ.AU: fjh set sender to devnull@stump.algebra.com using -f
User-Agent: tin/pre-1.4-980117 (UNIX) (OSF1/V4.0 (alpha))
Newsgroups: comp.std.c++
X-Complaints-To: abuse@demon.net

Ron Natalie <ron@sensor.com> wrote:

Seems it requires a default constructor for both A and B. Check the output
of the following program:

#include <stdio.h>

struct A {
        A() {v=0; printf("default A\n");}
        explicit A(int i) {v=i; printf("explicit A\n");}
int v;
};

struct B {
A v;
        B() {printf("default B\n");}
        B(A const &j) {v=j; printf("non-default B\n");}
};

struct C {
B v;
        C(B const &k) {v=k; printf("In C\n");}
};

int main()
{
        C c(B(A(1)));
        return 1;
}


The output is:
explicit A
default A
non-default B
default A
default B
In C

Both default A and default B constructors are called in the
process. And seems GNU C++ doesn't provide default constructor. :)


Feng Chen
fchen@fas.harvard.edu


: Ulf Larsson wrote:

:> g++-2.95.2 tells me that
:> 
:> prog.cc: In function `void f()':
:> prog.cc:19: no matching function for call to `B::B (int)'
:> prog.cc:8: candidates are: B::B(const A &)
:> prog.cc:9:                 B::B(const B &)
:> 
:> This doesn't make sense to me.
: ]
: Doesn't make sense to me either.

: Works fine in egcs 2.91.57 (and the Spar Compiler 5.0)

: ---
: [ comp.std.c++ is moderated.  To submit articles, try just posting with ]
: [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
: [              --- Please see the FAQ before posting. ---               ]
: [ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]


======================================= MODERATOR'S COMMENT: 

Please do not overquote when posting followup message.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]




