473,761 Members | 9,474 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7843


tz***@phonedo.c om 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******@gasca d.at> wrote in message
news:3E******** *******@gascad. at...


tz***@phonedo.c om 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******@gasca d.at> wrote...


tz***@phonedo.c om 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******@gasca d.at> wrote...


tz***@phonedo.c om 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******@gasca d.at> wrote...


Victor Bazarov wrote:

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


tz***@phonedo.c om 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
1494
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, the constructor call will still > | call its __init__ method. If you return an object of a different
9
46591
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 following simple illegal C++ program: class Listener { public: virtual void onEvent(int n) = 0;
23
5182
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
9972
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 method. The method could look like this: public void myDynamicMethod(Type type){ //.....do something with the Type passed in....... }
45
6362
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 parameters, I get CS1501 (no method with X arguments). Here's a simplified example which mimics the circumstances: namespace InheritError { // Random base class. public class A { protected int i;
6
5128
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 performed on the same thread thread that instantiated the class. One way to do this is to pass an ISynchronizeInvoke into the class and use it to synchronize the callback. In the constructor of the class, could I take note of the current thread...
12
7211
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? class Trial { public: Trial() {
44
2950
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 class is used as a function that keeps state from one call to the next. The problem is that I don't know what to call such a thing! "Abstract class" isn't right, because that implies that you should subclass the class and then instantiate the...
275
12370
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
9376
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9923
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9811
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8813
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7358
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5266
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3911
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3509
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.