This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Strange behaviour in C++...



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;        
}




Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]