This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Strange behaviour in C++...
- To: Fredrik Öhrström <d92-foh at nada dot kth dot se>, gcc at gcc dot gnu dot org
- Subject: Re: Strange behaviour in C++...
- From: "Christopher R. Jones" <cj at interlog dot com>
- Date: Tue, 24 Aug 1999 11:56:45 -0400
Using Borland 5.0 on WinNT:
Alfa 1=42
Alfa 2=42
BetaAlfa=42
Alfa 1=47
Alfa 2=47
GammaAlfa=47
AlfaGammaAlfa=47
>
>The code below compiled with 2.95.1 (19990816)
>(on a RedHat 5.2 system i386) produces:
>
>Alfa 1=42
>Alfa 2=42
>BetaAlfa=42
>Alfa 1=47
>Alfa 2=134580865
>GammaAlfa=47
>AlfaGammaAlfa=47
>
>Shouldn't Alfa 2 be 47 in the second creation?
>
>Cheers
>Fredrik
>
>---------------------------------------------------------
>
>
>#include<iostream.h>
>
>struct Alfa
>{
> virtual int alfa() = 0;
>};
>
>struct Beta
>{};
>
>struct Gamma
>{};
>
>struct Alfa_i : public virtual Alfa
>{
> int a;
>
> Alfa_i (int aa) : a (aa)
> {
> }
>
> int alfa ()
> {
> return a;
> }
>};
>
>struct Beta_i : public virtual Beta,
> public Alfa_i
>{
> Beta_i (int bb) : Alfa_i (bb)
> {
> cerr << "Alfa 1=" << alfa ()<< endl;
> Alfa *a = this;
> cerr << "Alfa 2=" << a->alfa ()<< endl;
> }
>};
>
>struct Gamma_i : public virtual Gamma,
> public Beta_i
>{
> Gamma_i (int cc)
> : Beta_i (cc)
> {}
>};
>
>
>int main ()
>{
> Beta_i *b = new Beta_i (42);
> cerr << "BetaAlfa=" << b->alfa() << endl;
>
> Gamma_i *g = new Gamma_i (47);
> cerr << "GammaAlfa=" << g->alfa() << endl;
>
> Alfa *a = g;
> cerr << "AlfaGammaAlfa=" << a->alfa() << endl;
>}
>
>
>
>
>
Christopher R. Jones, P.Eng.
14 Oneida Avenue
Toronto, Ontario M5J 2E3
Tel. 416 203-7465
Fax. 416 203-3044
Email cj@interlog.com