Call constructor on an already existing instance?

Discussion in 'C++' started by Karl Heinz Buchegger, Jun 25, 2003.

  1. 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
     
    Karl Heinz Buchegger, Jun 25, 2003
    #1
    1. Advertising

  2. Karl Heinz Buchegger

    Guest

    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.
     
    , Jun 25, 2003
    #2
    1. Advertising

  3. Karl Heinz Buchegger

    MK Guest

    "Karl Heinz Buchegger" <> wrote in message
    news:...
    >
    >
    > 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
    >

    "placement new" works on a raw memory, not an instance.
    I think it's undefined when "placement new" works on an instance.
     
    MK, Jun 25, 2003
    #3
  4. "Karl Heinz Buchegger" <> wrote...
    >
    >
    > 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
     
    Victor Bazarov, Jun 25, 2003
    #4
  5. Victor Bazarov wrote:
    >
    > "Karl Heinz Buchegger" <> wrote...
    > >
    > >
    > > 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
     
    Karl Heinz Buchegger, Jun 26, 2003
    #5
  6. "Karl Heinz Buchegger" <> wrote...
    >
    >
    > Victor Bazarov wrote:
    > >
    > > "Karl Heinz Buchegger" <> wrote...
    > > >
    > > >
    > > > 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
     
    Victor Bazarov, Jun 26, 2003
    #6
  7. 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
     
    Karl Heinz Buchegger, Jun 26, 2003
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marauderz
    Replies:
    1
    Views:
    1,230
    Trevor Benedict R
    Jun 10, 2004
  2. Yadagiri Rao KP
    Replies:
    1
    Views:
    356
    =?ISO-8859-1?Q?Daniel_Sj=F6blom?=
    Aug 10, 2003
  3. Sandra-24
    Replies:
    18
    Views:
    1,921
    Steve Holden
    Apr 29, 2006
  4. Generic Usenet Account
    Replies:
    10
    Views:
    2,320
  5. Olive
    Replies:
    4
    Views:
    251
    Olive
    Jul 6, 2012
Loading...

Share This Page