From: gordie@mcode.amdahl.com (Gordon Freedman)
Newsgroups: comp.lang.c++
Subject: Re: question re this and new
Date: 9 Apr 92 00:30:17 GMT
Reply-To: gjf00@duts.ccc.amdahl.com
Organization: Amdahl Corporation, Sunnyvale Ca.

In article <1992Apr7.232411.1337@elroy.jpl.nasa.gov>, alan@elroy.jpl.nasa.gov (Alan S. Mazer) writes:
> If I have a virtual base class (with multiple inheritance) shouldn't the 
> value of "this" in the constructor of a derived class be the same value
> eventually returned by "new"?  It's not ("new" returns something 8 bytes
> greater than "this" was in the constructor) and I'm not sure why.
> -- 
> 
> -- Alan
>    alan@elroy.jpl.nasa.gov

I thought I knew the answer to this, but after compiling and running
the following programs (the comments are the results), I realized I
don't. Perhaps this example will help somebody shed some light on this.

------------cut for program-----------------
#include <stdio.h>

class parent {
private:
    int i ;
    char c ;
public:
    parent (int n = 999)
    {
        i = n;
        ::fprintf (stderr, "My address is %x\n", this) ;
        ::fprintf (stderr, "addr of i is %x %x\n", &i, & this -> i) ;
    }
    int * ai () { return &i ;}
    void print () { ::fprintf (stderr, "I am at %x, Value of i is %d\n",
        this, i) ; }
} ;

class child : public virtual parent {
private:
    int j ;
    float f ;
public:
} ;

int
main (int argc, char ** argv)
{
    child c ;
	// My address is f7fff94c
	//addr of i is f7fff94c f7fff94c
    parent * p = & c ;
    int * ip ;

    c.print () ;
	// I am at f7fff94c, Value of i is 999
    p -> print () ;
	// I am at f7fff94c, Value of i is 999
    fprintf (stderr, "c address is %x, cast to parent is %x, p is %x\n",
        &c, &((parent) c), p) ;
	// c address is f7fff940, cast to parent is f7fff940, p is f7fff94c
    ip = c.ai () ;
    fprintf (stderr, "Address of i is %x\n", ip) ;
	// Address of i is f7fff94c
    * ip = 25 ;
    c.print () ;
	// I am at f7fff94c, Value of i is 25
}

----------------------------

To make things more confusing, look what happens when the child
inherits "unvirtually" ...

-----------------Child does not inherit from virtual parent----------

#include <stdio.h>

class parent {
private:
    int i ;
    char c ;
public:
    parent (int n = 999)
    {
        i = n;
        ::fprintf (stderr, "My address is %x\n", this) ;
        ::fprintf (stderr, "addr of i is %x %x\n", &i, & this -> i) ;
    }
    int * ai () { return &i ;}
    void print () { ::fprintf (stderr, "I am at %x, Value of i is %d\n",
        this, i) ; }
} ;

// *** LOOK HERE ***
// *** LOOK HERE ***
//
// instead of public virtual parent ...
//
// *** LOOK HERE ***
// *** LOOK HERE ***
class child : public parent {
private:
    int j ;
    float f ;
public:
} ;

int
main (int argc, char ** argv)
{
    child c ;
	// My address is f7fff948
	// addr of i is f7fff948 f7fff948
    parent * p = & c ;
    int * ip ;

    c.print () ;
	// I am at f7fff948, Value of i is 999
    p -> print () ;
	// I am at f7fff948, Value of i is 999
    fprintf (stderr, "c address is %x, cast to parent is %x, p is %x\n",
        &c, &((parent) c), p) ;
	// c address is f7fff948, cast to parent is f7fff948, p is f7fff948
    ip = c.ai () ;
    fprintf (stderr, "Address of i is %x\n", ip) ;
	// Address of i is f7fff948
    * ip = 25 ;
    c.print () ;
	// I am at f7fff948, Value of i is 25
}

-------------------------------------------------------------------

Gordon Freedman: gjf00@duts.ccc.amdahl.com  -> Dis: DAT
The difference between theory and practise in practise is greater than
the difference between theory and practise in theory - someone else's sig