Victor Bazarov
10/5/2008 10:16:00 PM
Hendrik Schober wrote:
> James Kanze wrote:
>> On Oct 3, 6:17 pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
>>> The 'operator new' function is the class-wide allocation
>>> function. Before any object of that class can be constructed
>>> in free store, the memory has to be allocated. That's why the
>>> allocation happens before the construction.
>>
>> Just a nit, but that last sentence applies to all objects, not
>> just those dynamically allocated. One of the particularities of
>> C++ is that there is NO syntax for calling a constructor without
>> formally allocating memory. [...]
>
> I don't doubt that you're right, but I'd like to know
> what this
> std::string("huh?")
> is if it's not the explicit invocation of a constructor.
It is a creation of a temporary object, which of course *causes* a call
to the constructor (and later a destructor at the end of the object's
lifetime, which is impossible for you to prevent, BTW).
You *may* call it an "explicit invocation", and start beating that dead
horse again, but let me just make a note here: if it's an invocation,
it's not explicit. The constructor is *invoked* or *called* by the
execution environment when you *create an object*. The language of the
Standard is such that there cannot be *direct* call to the constructor
simply because constructors do not have names and cannot be found during
lookup.
No, back to the subject at hand. James said you cannot call (or invoke,
if you will) a constructor without allocating memory first, and not that
you can't call a constructor (or cause its invocation). When you do
std::string("bleh")
the system will *still* allocate memory for you first, and then invoke
the constructor to construct the object in that memory. You do not call
the constructor. You do not allocate memory. The syntax is for object
creation, and you give the system the responsibility for the low-level
stuff.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask