Topic: Casts to reference


Author: bboreham@csfp.co.uk (Bryan Boreham)
Date: 1995/09/27
Raw View
Is anyone else alarmed that a cast to a reference type can silently
convert anything, even when the relevant object or pointer cast would
be disallowed?

E.g.:

  struct { double x; } s;
  double d = (double)s;     // Error: Cannot cast from struct to double.
  double &e = (double&)s;   // this is OK

  char *p = (char*)s;       // Error: Cannot cast from struct to char*.
  char &q = (char&)s;       // fine
  char*&r = (char*&)s;      // no problem.

  class A {};
  class B : public virtual A {};

  A* a;
  B* b = (B*)a;          // Error: Cannot cast from A* to B*.
  B& c = (B&)a;          // this is OK

I get the same answer from all the compilers I have available.  Even
the ARM seems to rule out the last one, but the compilers don't mind.

Newcomers may be taught that references are "just like objects except
cheaper to copy", or "just like pointers except you don't have to
dereference them".  As far as casting goes, references are dangerous.

Bryan Boreham, Front Office Development, Credit Suisse Financial Products
1 Cabot Square, London E14 4QJ, phone: +44 171 516 2058, fax:+44 171 516 2688
email: bboreham@csfp.co.uk

---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]





Author: xavier <tarrago@lcus15.saclay.cea.fr>
Date: 1995/09/28
Raw View
bboreham@csfp.co.uk (Bryan Boreham) wrote:

>   struct { double x; } s;
>   double d = (double)s;     // Error: Cannot cast from struct to double.

You try to cast a struct to pointer
double* d = (double*)&s;      // I think it's OK

>   double &e = (double&)s;   // this is OK

This is equivalent

>
>   char *p = (char*)s;       // Error: Cannot cast from struct to char*.
>   char &q = (char&)s;       // fine
>   char*&r = (char*&)s;      // no problem.
>
>   class A {};
>   class B : public virtual A {};
>
>   A* a;
>   B* b = (B*)a;          // Error: Cannot cast from A* to B*.
>   B& c = (B&)a;          // this is OK

on HP C++ compiler it results in:
error: cast: A* ->derived B*; A is virtual base

I tried this one and I noticed that the error arise only when B inherits
VIRTUALY from A ie:

class A {};
class B : public  A {};
A* a;
B* b = (B*)a;          // this is OK
B& c = (B&)a;          // this is OK

I dont understand. There is ambiguity only if B inherits from B
NON VIRTUALY and more than one time.
class A{};
class B : public A {};
class C : public A {};
class D : public B, public C {};

A* a;
D* d = (D*)a;

I tried it and :
error: cast: A* ->derived D*; A is base more than once

** Why is this illegal with virtual inheritance ?
** Why is it legal with references ?

 X.Tarrago
---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]