473,396 Members | 2,057 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Call constructor on an already existing instance?

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.
Jul 19 '05 #1
6 7819


tz***@phonedo.com wrote:

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.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #2
MK
"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:3E***************@gascad.at...


tz***@phonedo.com wrote:

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.

--
Karl Heinz Buchegger
kb******@gascad.at

"placement new" works on a raw memory, not an instance.
I think it's undefined when "placement new" works on an instance.
Jul 19 '05 #3
"Karl Heinz Buchegger" <kb******@gascad.at> wrote...


tz***@phonedo.com wrote:

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.


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
Jul 19 '05 #4


Victor Bazarov wrote:

"Karl Heinz Buchegger" <kb******@gascad.at> wrote...


tz***@phonedo.com wrote:

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.


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?)

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #5
"Karl Heinz Buchegger" <kb******@gascad.at> wrote...


Victor Bazarov wrote:

"Karl Heinz Buchegger" <kb******@gascad.at> wrote...


tz***@phonedo.com wrote:
>
> 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.


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?)


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
Jul 19 '05 #6


Victor Bazarov wrote:

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.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Steven Bethard | last post by:
Felix Wiemann wrote: > Steven Bethard wrote: >> http://www.python.org/2.2.3/descrintro.html#__new__ > > > I'm just seeing that the web page says: > > | If you return an existing object,...
9
by: Dario | last post by:
This is a technical C++ post regarding the Microsoft runtime error R6025 Pure Virtual Function Call that sometime occurs in programs compiled with Microsoft Visual C++ 6.0. Please consider the...
23
by: Fabian Müller | last post by:
Hi all, my question is as follows: If have a class X and a class Y derived from X. Constructor of X is X(param1, param2) . Constructor of Y is Y(param1, ..., param4) .
7
by: Mark Miller | last post by:
I am using Reflection.Emit to dynamically build a class. A method of the class to be built requires a Parameter of type "Type". But I don't know how to use Emit to pass a call of "typeof()" to the...
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
6
by: HolyShea | last post by:
All, Not sure if this is possible or not - I've created a class which performs an asynchronous operation and provides notification when the operation is complete. I'd like the notification to be...
12
by: Rahul | last post by:
Hi Everyone, I have the following code and i'm able to invoke the destructor explicitly but not the constructor. and i get a compile time error when i invoke the constructor, why is this so? ...
44
by: Steven D'Aprano | last post by:
I have a class which is not intended to be instantiated. Instead of using the class to creating an instance and then operate on it, I use the class directly, with classmethods. Essentially, the...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.