Topic: valid code?


Author: ulf@jota.sm.luth.se (Ulf Larsson)
Date: 2000/03/18
Raw View
Hi

I fail to understand what is wrong with the code below.

struct A
{
  explicit A(int);
};

struct B
{
  B(A const &);
};

struct C
{
  C(B const &);
};

void
f()
{
  C c(B(A(1)));
}

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.

Is g++ correct in rejecting this code? If so, why?


Thanks

Ulf Larsson

---
[ 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              ]






Author: Ron Natalie <ron@sensor.com>
Date: 2000/03/18
Raw View

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              ]






Author: feng chen <fchen@fas.harvard.edu>
Date: 2000/03/21
Raw View
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              ]






Author: Ron Natalie <ron@sensor.com>
Date: 2000/03/21
Raw View

feng chen wrote:
>
> Ron Natalie <ron@sensor.com> wrote:
>
> Seems it requires a default constructor for both A and B. Check the output
> of the following program:
>

I never wrote any of this.  Please watch your attributions.

Of course it requires a default constructor for A.  The B constructors
don't provide any initilaizers for the v member otherwise.  The same with
C (not) initializing B.

I think what you're expeting is this:

struct A {
   int v;
   A() : v(0) { }
   explicit A(int i) : v(i) { }
};

struct B {
   A v;
   B(A const& j) : v(j) { };
};

struct C {
   B v;
   C(B const& j) : v(j) { }
};

Do you understand the difference between initialization and assignment?

---
[ 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              ]