"James Kanze" <> ha scritto nel
messaggionews:457b6fc5-4d9a-448b-af5b-ca10db4fa35b@a30g2000yqn.googlegroups.com...
"red floyd" <
[email protected]> ha scritto nel
messaggio[placement new example redacted]
* Will automatically construct the object
Which is fine when that's what you want.
?malloc too construct object it only not initialize it
Neither malloc nor the operator new function construct the
object. (In C++, "construct" and "initialize" are practically
synonyms.)
"construct" is not a "initialize" synonyms
It is when you're talking about C++. The constructor
"initializes" the object, and a constructed object is an
"initialized" object.
i would know if there is
int x;
x is "construct" [reserve the memory] but not initialized
in others words how do you would say that?
In this case, I might agree, although the C++ standard will
sometimes speak of x being initialized to an indeterminate (and
possibly invalid) value.
The important distinction is that a "constructed" (or
"initialized") object has the type of the object. If a type has
a non-trivial constructor, an object of that type doesn't start
to exist type until it has been constructed (or
initialized---the C++ standard often uses the two words
indiscriminately). Before the object is constructed, all you
have is raw memory. malloc and the operator new function return
raw memory; a new expression creates an (initialized) object.
so for you reserve the memory for one object type
(int, double, structs, classes) is not construct it?
Reserving the memory doesn't construct an object. It just
reserves raw memory. In the case of a type with a trivial
constructor, the lifetime of the object begins as soon as the
memory has been allocated. In the case of a type with
a non-trivial constructor, the lifetime of the object doesn't
begin until the constructor call is complete.
i would spell that in this way:
"initialize object" == write the memory of the object the first time
"construct object" == reserve the memory for the object
You could. But then you wouldn't be talking about C++.