Call constructor on an already existing instance?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tzach@phonedo.com

    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.
  • Karl Heinz Buchegger

    #2
    Re: Call constructor on an already existing instance?



    tzach@phonedo.c om wrote:[color=blue]
    >
    > 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?[/color]

    Lookup 'placement new' in your favorite text book.

    --
    Karl Heinz Buchegger
    kbuchegg@gascad .at

    Comment

    • MK

      #3
      Re: Call constructor on an already existing instance?

      "Karl Heinz Buchegger" <kbuchegg@gasca d.at> wrote in message
      news:3EF98FF7.9 42CAB97@gascad. at...[color=blue]
      >
      >
      > tzach@phonedo.c om wrote:[color=green]
      > >
      > > 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?[/color]
      >
      > Lookup 'placement new' in your favorite text book.
      >
      > --
      > Karl Heinz Buchegger
      > kbuchegg@gascad .at[/color]
      "placement new" works on a raw memory, not an instance.
      I think it's undefined when "placement new" works on an instance.


      Comment

      • Victor Bazarov

        #4
        Re: Call constructor on an already existing instance?

        "Karl Heinz Buchegger" <kbuchegg@gasca d.at> wrote...[color=blue]
        >
        >
        > tzach@phonedo.c om wrote:[color=green]
        > >
        > > 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?[/color]
        >
        > Lookup 'placement new' in your favorite text book.[/color]

        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


        Comment

        • Karl Heinz Buchegger

          #5
          Re: Call constructor on an already existing instance?



          Victor Bazarov wrote:[color=blue]
          >
          > "Karl Heinz Buchegger" <kbuchegg@gasca d.at> wrote...[color=green]
          > >
          > >
          > > tzach@phonedo.c om wrote:[color=darkred]
          > > >
          > > > 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?[/color]
          > >
          > > Lookup 'placement new' in your favorite text book.[/color]
          >
          > 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.
          >[/color]

          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
          kbuchegg@gascad .at

          Comment

          • Victor Bazarov

            #6
            Re: Call constructor on an already existing instance?

            "Karl Heinz Buchegger" <kbuchegg@gasca d.at> wrote...[color=blue]
            >
            >
            > Victor Bazarov wrote:[color=green]
            > >
            > > "Karl Heinz Buchegger" <kbuchegg@gasca d.at> wrote...[color=darkred]
            > > >
            > > >
            > > > tzach@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.[/color]
            > >
            > > 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.
            > >[/color]
            >
            > 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?)[/color]

            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


            Comment

            • Karl Heinz Buchegger

              #7
              Re: Call constructor on an already existing instance?



              Victor Bazarov wrote:[color=blue]
              >
              > 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.[/color]

              OK
              Thanks.

              --
              Karl Heinz Buchegger
              kbuchegg@gascad .at

              Comment

              Working...