home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!portal!cup.portal.com!Aurelius
- From: Aurelius@cup.portal.com (Mark Christian Barnes)
- Newsgroups: comp.lang.c++
- Subject: Re: complex classes and temporary destruction.
- Message-ID: <64821@cup.portal.com>
- Date: Thu, 27 Aug 92 23:44:12 PDT
- Organization: The Portal System (TM)
- Distribution: world
- References: <MCGRANT.92Aug26232410@rascals.stanford.edu>
- <23563@alice.att.com>
- Lines: 52
-
- Andrew Koenig writes:
-
- | Don't worry, the issues under discussion will not affect well-behaved
- | programs such as this. The real problems come about when, say, a String
- | class has operator const char() that returns a pointer to the internal
- | representation of the String. That then raises the problem:
- |
- | String s, t;
- |
- | // ...
- | const char* p = s + t;
- | and now, the question is when the temporary representing `s + t' is
- | destroyed. The point is that destroying that temporary invalidates p,
- | so it suddenly becomes an issue of great concern.
- | --
- | --Andrew Koenig
-
- Have the temporary "inherit" (for lack of a better word) the
- storage class of p. If p is an auto variable then 's+t' should
- be auto too. Destroy p and 's+t' together. If p is static, then
- perhaps have 's+t' be static as well, onc instance of 's+t'
- gets re-used each time the statement is executed.
-
- For global variables, you have to watch out for all kinds of
- unknowns. In this case, maybe a destructor should be called
- right before p is assigned a new value. This assumes that the
- old value of p is no longer needed and the new temporary is not
- able to re-use the 's+t' object. Also 's+t' may be assigned to more
- than one variable by this time, and therefore should remain around.
-
- String s = "hello"; // global String
- const char *pS1, *pS2; // global pointers
-
- void not_main ( void )
- {
- pS1 = s + s; // assign temporary to global
- pS2 = pS1; // save a copy
- pS1 = s + s + s; // assign another temp to global, re-use 's+s'?
- // or maybe destruct 's+s' and create 's+s+s'?
- // But no!, pS1 points to 's+s' also. Since
- // the pointers are always in scope, when
- // do the temporaries get destructed??
- }
-
- This bit of an example may show that the two "temporaries"
- could be destroyed or re-used before the assignment. But of
- course they may be aliased again outside the scope of this
- module. About all that makes sense to me is that they are
- not actually "temporary" at all, but are unnamed globals,
- whose usage needs to be tracked by the compiler.
-
- Regards, Aurelius@cup.portal.com
-