Call constructor on an already existing instance?

  • Thread starter Karl Heinz Buchegger
  • Start date
K

Karl Heinz Buchegger

Call constructor on an already existing instance?
I like to execute the constructor logic,
including all the class member constructors,
on an existing instance (with out executing the destructor).
Can it be?

Lookup 'placement new' in your favorite text book.
 
T

tzach

Call constructor on an already existing instance?
I like to execute the constructor logic,
including all the class member constructors,
on an existing instance (with out executing the destructor).
Can it be?
Thanks.
 
M

MK

Karl Heinz Buchegger said:
Lookup 'placement new' in your favorite text book.
"placement new" works on a raw memory, not an instance.
I think it's undefined when "placement new" works on an instance.
 
V

Victor Bazarov

Karl Heinz Buchegger said:
Lookup 'placement new' in your favorite text book.

According to language rules the number of calls to constructors
is supposed to match the number of calls to destructors. So,
what the OP wants would violate that. Whatever logic needs to
be executed should be put in a separate function.

Victor
 
K

Karl Heinz Buchegger

Victor said:
According to language rules the number of calls to constructors
is supposed to match the number of calls to destructors. So,
what the OP wants would violate that. Whatever logic needs to
be executed should be put in a separate function.

Thanks for pointing out.
Just for interest: Does the standard really have such a rule?
(Meaning: could you guide me to where to find it?)
 
V

Victor Bazarov

Karl Heinz Buchegger said:
Thanks for pointing out.
Just for interest: Does the standard really have such a rule?
(Meaning: could you guide me to where to find it?)

If you look at 3.8 Object Lifetime, you'd notice that in the example
on the page 50 (after paragraph 7) the technique with constructing
an object "on top of" or "in place of" another one is given _almost_
like the one you hinted at. However, a destructor is called first.
The requirement is that the storage can be reused if the lifetime of
the object that occupies that storage has ended. Another definition
is that the lifetime starts when constructor returns and ends when
the destructor is called. That suggests that to have a proper C++
program every object has to have clean lifetime: c-tor through d-tor.

There is, however, a diviation from that requirement, see 3.8/4.
The Standard says that the program may end the lifetime by reusing
the storage and that it's not required to call a destructor, but
undefined behaviour may occur if it doesn't.

Victor
 
K

Karl Heinz Buchegger

Victor said:
If you look at 3.8 Object Lifetime, you'd notice that in the example
on the page 50 (after paragraph 7) the technique with constructing
an object "on top of" or "in place of" another one is given _almost_
like the one you hinted at. However, a destructor is called first.
The requirement is that the storage can be reused if the lifetime of
the object that occupies that storage has ended. Another definition
is that the lifetime starts when constructor returns and ends when
the destructor is called. That suggests that to have a proper C++
program every object has to have clean lifetime: c-tor through d-tor.

There is, however, a diviation from that requirement, see 3.8/4.
The Standard says that the program may end the lifetime by reusing
the storage and that it's not required to call a destructor, but
undefined behaviour may occur if it doesn't.

OK
Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top