This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Problem with base and derived classes.


On Aug 30, 1999, Alexander Zvyagin <zvyagin@gams.ihep.su> wrote:

>   D d1(B()); // The compiler message is expected here if D is undefined.

This is not a declaration of an object d1, of type D, initialized with
a temporary B().  According to the C++ grammar and disambiguation
rules, this is a declaration of a function named d1, returning a D,
and taking an argument of type `B()', i.e., a pointer (implicitly
inferred) to a function taking no arguments and returning a B.
Exactly as gcc reports:

> file1.c:12: assignment to `A *' from `D (*)(B (*)())'

There are two possible work-arounds.  The first, albeit cleaner, has
slightly different semantics, since it relies on copy-construction:

D d1 = B();

In order to perform direct initialization, as you want, a common
disambiguation in favor of an expression initializer, over a type
name, is:

D d1((0,B()));

The extra 0, even being discarded because of the comma operator, tells
the parser an expression follows, and not a type name.

-- 
Alexandre Oliva http://www.dcc.unicamp.br/~oliva IC-Unicamp, Bra[sz]il
oliva@{dcc.unicamp.br,guarana.{org,com}} aoliva@{acm.org,computer.org}
oliva@{gnu.org,kaffe.org,{egcs,sourceware}.cygnus.com,samba.org}
** I may forward mail about projects to mailing lists; please use them


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]