πŸš€ go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

3 total messages Started by Bryan Boreham Wed, 27 Sep 1995 16:54
Casts to reference
#2051
Author: Bryan Boreham
Date: Wed, 27 Sep 1995 16:54
37 lines
1732 bytes
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. ]
Re: Casts to reference
#2058
Author: Steve Clamage
Date: Wed, 27 Sep 1995 18:49
38 lines
1945 bytes
In article 95Sep27095444@physics10.Berkeley.EDU, bboreham@csfp.co.uk (Bryan Boreham) writes:
>
>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?

No, and the reference cast is allowed only if the comparable pointer cast
is allowed anyway. In any event, you should not use old-style casts at all.

If you use static_cast or dynamic_cast, the cast will be permitted only
if it is safe. Otherwise it will fail at compile time with an error, or
at run time with a null return or a "bad_cast¨ exception.


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

The pointer cast may be a programming error, but is allowed by the language
rules. The old-style pointer cast says "Trust me, I know what I am doing."
The compiler must allow it.

>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.

No, old-style casts are dangerous, period. References are merely not an exception.
---
Steve Clamage, stephen.clamage@eng.sun.com



---
[ 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. ]
Re: Casts to reference
#2061
Author: xavier
Date: Thu, 28 Sep 1995 09:48
57 lines
1994 bytes
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. ]
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads