Topic: order of new, constructor call, and assignment
Author: "Martijn Lievaart" <mlievaar@orion.nl>
Date: 1998/02/16 Raw View
Matt Armstrong wrote in message ...
>Given this code:
>
>MyObject* globalVar = 0;
>
>void MyFunction()
>{
> globalVar = new MyObject;
>}
>
>Does the standard have anything to say about the value of globalVar
>within MyObject's constructor? Is it well defined to be 0, or does
>the standad allow globalVar to be assigned the return value of new
>_before_ the constructor returns?
>
Apart from what the standard says, consider this.
new MyObject is an expression returning an pointer to an instance of
MyObject. Maybe an implementation could assign this value to globalvar
before the constructor is called, but what about the following examples
(never mind if they are sensible, they're legal C++):
1) delete new MyObject;
2) x = (new MyObject)->Foo();
Clearly here the constructor must be called before the value is used for
these statements to do what they where intended to do.
Therefore I find it hard to imagine global operator=() to behave different,
apart from whatever the standard says.
In other words, the implementation of "a=new T;" would be an exception to
all other assignments and to all other uses of new. I find this very
illogical.
But then, illogical things do happen don't they?
Martijn Lievaart, mlievaar(at)orion(dot)nl
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Matt Armstrong <mattdav@shell4.ba.best.com>
Date: 1998/02/05 Raw View
Given this code:
MyObject* globalVar = 0;
void MyFunction()
{
globalVar = new MyObject;
}
Does the standard have anything to say about the value of globalVar
within MyObject's constructor? Is it well defined to be 0, or does
the standad allow globalVar to be assigned the return value of new
_before_ the constructor returns?
(Please ignore the obvious problems with code that depends on one
behavior or the other, or theorize that the answer shouldn't matter
because of the obvious problems with such code (unless that is the
rationale the standard takes)).
Thanks!
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]