Article View: comp.std.c++
Article #2061Re: Casts to reference
From: xavier
Date: Thu, 28 Sep 1995 09:48
Date: Thu, 28 Sep 1995 09:48
57 lines
1994 bytes
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. ]
Technical Details
Message-ID:
<44dqqj$g94@anemone.saclay.cea.fr>Path:
arc1-ca.pugleaf.net!in2.uu.net!bounce-backReferences:
<MATT.95Sep27095444@physics10.Berkeley.EDU>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!in2.uu.net!bounce-back Date: 28 Sep 95 09:48:46 GMT Approved: fjh@cs.mu.oz.au From: xavier <tarrago@lcus15.saclay.cea.fr> Newsgroups: comp.std.c++ Subject: Re: Casts to reference X-Original-Date: 28 Sep 1995 09:44:51 GMT Organization: CEA Commissariat a l'Energie Atomique, France. Message-ID: <44dqqj$g94@anemone.saclay.cea.fr> References: <MATT.95Sep27095444@physics10.Berkeley.EDU>