[comp.lang.c++] value of 'this' in constructor

gt3930b@prism.gatech.EDU (NICHOLS,STEVEN MALCOLM) (08/17/90)

	Consider the following piece of code.  From p. 284 of "Using
C++", and also p. 164 of "The C++ Programming Language", I am lead to 
believe that the value of the pointer 'this', on entry to a constructor,
will be 0 if the instance of the class has been dynamically allocated.
If the class has been statically allocated, the value of 'this' will be
non-zero.  However, I see non-zero values for this for both static and dynamic
instances of the class test in the code fragment that follows. 
	I see this behavior for both ZORTECH v. 2.06 on a 386 based PC, and
a similar output for the same code compiled using Oregon C++ on a VMS based
DEC micro-vax.  Since this is happening for *two* compilers, I conclude that
I have misunderstood Eckel and Stroustrup when they described this feature.
	If the error is in my understanding, or is well known, please e-mail
me at gt3930b@prism.gatech.edu; if it is a bona fide bug, or a non-trivial
language issue, please post for others to read.


// the code sample, set up for ZORTECH
#include <stream.hpp>

class test
{
   test* this_on_entry;
public:
   test() { this_on_entry = this; }
   void show() {cout << "this_on_entry = " << (unsigned)this_on_entry << "\n"; }
};

main()
{
   test static_test;
   test* dynam_test;

      dynam_test = new test;
      cout << "static:";
      static_test.show();
      cout << "dynamic:";
      dynam_test->show();
}



// the output from ZORTECH
static:this_on_entry = 9128
dynamic:this_on_entry = 9170


thanks for the help,
steve nichols.
gt3930b@prism.gatech.edu

ark@alice.UUCP (Andrew Koenig) (08/17/90)

In article <12680@hydra.gatech.EDU>, gt3930b@prism.gatech.EDU (NICHOLS,STEVEN MALCOLM) writes:

> 	Consider the following piece of code.  From p. 284 of "Using
> C++", and also p. 164 of "The C++ Programming Language", I am lead to 
> believe that the value of the pointer 'this', on entry to a constructor,
> will be 0 if the instance of the class has been dynamically allocated.

If you are using the now-deprecated feature of assigning to `this,'
and you are using a compiler that supports the feature, then
`this' will be 0 on entry to your constructor -- BUT ONLY IF THE
CONSTRUCTOR ASSIGNS TO `this' (believe it or not!).

That is, the presence of an assignment to `this' in a constructor
makes that constructor magic: not only is `this' then zero on entry
to the constructor but you are obliged to assign some value to it
on *every* path through the constructor.  The following is typical:

	Foo::Foo() {
		if (this == 0) {
			this = /* some appropriate pointer */
		} else {
			this = this;	// believe it or not
		}

		// now you can deal with members of the object
	}

The idea is that the act of assigning to `this' triggers initialization
of base classes, and so on.

You see why the feature is deprecated?
-- 
				--Andrew Koenig
				  ark@europa.att.com

bright@Data-IO.COM (Walter Bright) (08/18/90)

In article <12680@hydra.gatech.EDU> gt3930b@prism.gatech.EDU (NICHOLS,STEVEN MALCOLM) writes:
<	Consider the following piece of code.  From p. 284 of "Using
<C++", and also p. 164 of "The C++ Programming Language", I am lead to 
<believe that the value of the pointer 'this', on entry to a constructor,
<will be 0 if the instance of the class has been dynamically allocated.
<If the class has been statically allocated, the value of 'this' will be
<non-zero.  However, I see non-zero values for this for both static and dynamic
<instances of the class test in the code fragment that follows. 
<#include <stream.hpp<
<class test
<{
<   test* this_on_entry;
<public:
<   test() { this_on_entry = this; }

This will not give the value of 'this' upon entry, because the compiler
inserts a call to new() before any user statements in the constructor.

<   void show() {cout << "this_on_entry = " << (unsigned)this_on_entry << "\n"; }
<};
<
<main()
<{
<   test static_test;
<   test* dynam_test;
<
<      dynam_test = new test;
<      cout << "static:";
<      static_test.show();
<      cout << "dynamic:";
<      dynam_test-<show();
<}
<
<// the output from ZORTECH
<static:this_on_entry = 9128
<dynamic:this_on_entry = 9170

rfg@NCD.COM (Ron Guilmette) (08/18/90)

In article <12680@hydra.gatech.EDU> gt3930b@prism.gatech.EDU (NICHOLS,STEVEN MALCOLM) writes:
<
<	Consider the following piece of code.  From p. 284 of "Using
<C++", and also p. 164 of "The C++ Programming Language", I am lead to 
<believe that the value of the pointer 'this', on entry to a constructor,
<will be 0 if the instance of the class has been dynamically allocated.
<If the class has been statically allocated, the value of 'this' will be
<non-zero.  However, I see non-zero values for this for both static and dynamic
<instances of the class test in the code fragment that follows. 
<	I see this behavior for both ZORTECH v. 2.06 on a 386 based PC, and
<a similar output for the same code compiled using Oregon C++ on a VMS based
<DEC micro-vax.  Since this is happening for *two* compilers, I conclude that
<I have misunderstood Eckel and Stroustrup when they described this feature.

Times change.  So do languages.  The old conventions regarding the value
of `this' upon entry to a constructor (which may assign to `this') don't apply
anymore.  A look at the newer "Annotated C++ Reference Manual" may help
you to understand the changes that "modern" C++ compilers are implementing
these days.

-- 

// Ron Guilmette  -  C++ Entomologist
// Internet: rfg@ncd.com      uucp: ...uunet!lupine!rfg
// Motto:  If it sticks, force it.  If it breaks, it needed replacing anyway.