Path: utzoo!attcan!uunet!husc6!cmcl2!rutgers!att!ulysses!andante!alice!ark From: ark@alice.UUCP (Andrew Koenig) Newsgroups: comp.lang.c++ Subject: Re: Type Compatibility Message-ID: <8810@alice.UUCP> Date: 21 Jan 89 05:22:53 GMT References: <5485@mit-vax.LCS.MIT.EDU> Organization: AT&T Bell Laboratories, Liberty Corner NJ Lines: 53 In article <5485@mit-vax.LCS.MIT.EDU>, waldemar@mit-vax.LCS.MIT.EDU (Waldemar Horwat) writes: > Given the declarations below, which of the following seven statements > (numbered 1, 2, 4, 5, 6, 7, and 8) are acceptable in a "portable" C++ program? > struct A {int x;}; > struct B:A {int y;}; > extern void f1(void *); > extern void g1(A *); > extern void i1(A &); > extern void f2(void **); > extern void g2(A **); > extern void h2(void *&); > extern void i2(A *&); > B b,*pb,**ppb; Here are my comments, along with the messages that appear when I run it on the (still experimental) cfront on my machine. > void test() > { > f1(pb); //1 OK. f1 takes a void* argument so you can give it a pointer to anything at all. > g1(pb); //2 OK. g1 takes an A*, you've given it a B*, and B is (publicly) derived from A. > i1(b); //4 OK. References are just like pointers. i1 takes an A&, you've given it a B&. > f2(ppb); //5 bad argument 1 type for f2(): B ** (void ** expected) No. You can convert anything to void* but you can't convert anything to void**. f2 takes a void** and ppb is a B**. > g2(ppb); //6 bad argument 1 type for g2(): B ** (A ** expected) No. Again, you can't treat a pointer to an B* as if if it were a pointer to an A*. > h2(pb); //7 warning: coercion of lvalue needed: temporary used to initialize reference Yes (!). h2 wants a reference to a void* and you've given it a B*. So it converts the B* to a void* (no problem there) and binds the reference to that. > i2(pb); //8 warning: coercion of lvalue needed: temporary used to initialize reference Yes -- same reason. You can convert pb to an A* and then bind a reference to it. > } -- --Andrew Koenig ark@europa.att.com