Path: utzoo!news-server.csri.toronto.edu!cs.utexas.edu!uunet!olivea!decwrl!world!wmm From: wmm@world.std.com (William M Miller) Newsgroups: comp.lang.c++ Subject: Re: Inheritance and references question Message-ID: <1991Mar5.010600.19687@world.std.com> Date: 5 Mar 91 01:06:00 GMT References: <1991Feb26.060459.6777@csun.edu| <71032@microsoft.UUCP> Organization: The World Public Access UNIX, Brookline, MA Lines: 47 jimad@microsoft.UUCP (Jim ADCOCK) writes: > I don't like this definition, mind you. I'd like the pertinent section > in 8.4.3 to say: > > If the initializer for a reference to type T is an lvalue of type T or of > a type derived from T for which T is an accessible base or if T and the > initializer are pointers or references to such, then the reference > will refer to the initializer... NO. This is a BAD idea and opens a gaping hole in the type system as bad as C's allowing assignment of void* pointers to typed pointers without casts. Consider the following example: class B {...}; class D: public B {...}; B B_glob; void get_a_B(B*& Bp) { Bp = &B_glob; } void f() { D* dp; get_a_B(dp); // illegal under current definition } Under your proposed definition, this would be perfectly legal and leave "dp" pointing at a B object -- a sure recipe for disaster. The whole idea behind the C++ type system is that if you do type-insecure things, you ought to have to use an explicit cast to indicate that you know what you're doing; there are no casts in the above code, even though it's terribly type-insecure. In cases where you really do want to be able to do what "common sense would seem to indicate," you can use an explicit cast: D* dp; B*& rbp = (B*&) dp; As I said in an earlier posting on this subject, you can get some insight into why an exact match is required by considering int and long -- even though you can convert an int into a long, just as you can a D* into a B*, that doesn't mean that you should be able to initialize a long& with an int; similarly, you can't initialize a B*& with a D*. -- William M. Miller, Glockenspiel, Ltd. wmm@world.std.com