[gnu.g++.bug] Can't assign 'this' in a constructor

patrick@cos.com (Patrick Steranka) (12/03/89)

In BS, pg 164, 5.5.6 it states:

	The assignment to "this" [in a constructor function] informs
	the compiler that the programmer has taken control and that the
	default mechanism for allocating storage should not be used.

This does not seem to be supported by the GNU g++ compiler.
Instead, the error

	assignment of read-only parameter `$this'

is produced.  Below is a sample program and the output produced
when compiling it.  This happens on a SUN 3/280 running SUN OS
3.5 using g++(1.36.1).

==============================   ex7.cc  ==============================
#include <stream.h>
class T{
    char s[10];
 public:
    T() {
	if (this == 0) this = (T*) 0x43;
	cout << "Value of 'this' is " << (int)this << "\n";
    };
    ~T() {
	cout << "In ~T, the value of 'this' is " << (int)this << "\n";
        this = 0;
    }
};

main(int argc, char* argv[])
{
    T a1;
}
==============================   ex7.cc  ==============================


====================  Output from compiling ex7.cc  ====================
cd /usr.MC68020/zulu/patrick/test/c++/
make -k ex7
g++ -o ex7 -g -v ex7.cc
gcc version 1.36.1- (based on GCC 1.36)
 /usr/local/lib/gnu/gcc/gcc-cpp -+ -v -undef -D__GNUC__ -D__GNUG__ -D__cplusplus -Dmc68000 -Dsun -Dunix -D__mc68000__ -D__sun__ -D__unix__ -D__HAVE_68881__ -Dmc68020 ex7.cc /usr/tmp/cca21893.cpp
GNU CPP version 1.36
 /usr/local/lib/gnu/gcc/gcc-cc1plus /usr/tmp/cca21893.cpp -quiet -dumpbase ex7.cc -g -version -o /usr/tmp/cca21893.s
GNU C++ version 1.36.1 (based on GCC 1.36) (68k, MIT syntax) compiled by GNU C version 1.36.
default target switches: -m68020 -mc68020 -m68881 -mbitfield
ex7.cc: In method T::T ():
ex7.cc:6: assignment of read-only parameter `$this'
*** Error code 1
make: Fatal error: Target `ex7' not remade because of errors

Compilation exited abnormally with code 1 at Sat Dec  2 17:25:34
====================  Output from compiling ex7.cc  ====================


patrick (Patrick Steranka @ Corporation for Open Systems)
	-- patrick@cos.com
	-- patrick%cos.com@uunet.uu.net
	-- {uunet, sundc, decuac, hqda-ai, hadron}!cos!patrick

schmidt@zola.ics.uci.edu (Doug Schmidt) (12/03/89)

In article <8912022231.AA26818@qna.cos.com>, patrick@cos (Patrick Steranka) writes:
>In BS, pg 164, 5.5.6 it states:
>
>	The assignment to "this" [in a constructor function] informs
>	the compiler that the programmer has taken control and that the
>	default mechanism for allocating storage should not be used.
>
>This does not seem to be supported by the GNU g++ compiler.
>Instead, the error
>
>	assignment of read-only parameter `$this'

try using the -fthis-is-variable flag.

Doug
--
Any man's death diminishes me,              | schmidt@ics.uci.edu (ARPA)
Because I am involved in Mankind;           | office: (714) 856-4043
And therefore never send to know for whom the bell tolls;
It tolls for thee        -- John Donne

tiemann@LURCH.STANFORD.EDU (Michael Tiemann) (12/03/89)

If you want to assign to `this', you must use the flag
-fthis-is-variable.  The 2.0 language spec permits GNU C++ to be picky
about this matter, though it suggests compiler writers go easy on
their users by giving the a flag whereby original behavior is
restored.  GNU C++ has never allowed `this' to be assigned outside of
constructors and destructors, so there is no behvior to restore there.

Michael