Explicit ctor/dtor calls

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jacques Labuschagne

    Explicit ctor/dtor calls

    Hi all,
    Is it legal to kill an object and build a new one of the same type in its
    memory?

    class A{
    int i;
    public:
    explicit A(int value): i(value){}
    };

    int main(){
    A a(3);
    a.~A();
    new (&a)A(2);
    }

    --
    There is this special biologist word we
    use for 'stable.' It is 'dead.' -- Jack Cohen

  • John Harrison

    #2
    Re: Explicit ctor/dtor calls


    "Jacques Labuschagne" <jacques@clawsh rimp.com> wrote in message
    news:1840039.ZJ xocY03RF@klesk. ..[color=blue]
    > Hi all,
    > Is it legal to kill an object and build a new one of the same type in its
    > memory?
    >
    > class A{
    > int i;
    > public:
    > explicit A(int value): i(value){}
    > };
    >
    > int main(){
    > A a(3);
    > a.~A();
    > new (&a)A(2);
    > }
    >[/color]

    Seems OK to me. There are a few issues here but none that you've raised in
    your example code.

    john


    Comment

    • Janusz Szpilewski

      #3
      Re: Explicit ctor/dtor calls

      Jacques Labuschagne wrote:[color=blue]
      > Hi all,
      > Is it legal to kill an object and build a new one of the same type in its
      > memory?
      >
      > class A{
      > int i;
      > public:
      > explicit A(int value): i(value){}
      > };
      >
      > int main(){
      > A a(3);
      > a.~A();
      > new (&a)A(2);
      > }
      >[/color]

      Yes, it is legal. Such a case is described in the C++ standard (3.8/7).
      The name of the object and any pointers or references to it still remain
      valid after such an operation.

      Comment

      • Michael Kochetkov

        #4
        Re: Explicit ctor/dtor calls


        "Jacques Labuschagne" <jacques@clawsh rimp.com> wrote in message
        news:1840039.ZJ xocY03RF@klesk. ..[color=blue]
        > Hi all,
        > Is it legal to kill an object and build a new one of the same type in its
        > memory?
        >
        > class A{
        > int i;
        > public:
        > explicit A(int value): i(value){}
        > };
        >
        > int main(){
        > A a(3);
        > a.~A();
        > new (&a)A(2);
        > }[/color]
        Let us suppose the first constructor succeeds and the second fails with the
        exception (it is not your case indeed but let us debate in general). A
        compiler must call the destructor for the successfully created a(3) local
        variable when it leaves the scope and he knows that the construction was
        successful. But your unsuccessful placement new really confuses it and, I
        believe, you may expect crashes, because if the second construction throws
        no destructor shall be called but it would be called for a(3).
        So, IMO, your solution may appear to be dangerous in general and I would
        recommend you to use the traditional approach of using a buffer of proper
        amount of chars with the subsequent placement new if you are interested in
        the final result.

        --
        With regards,
        Michael Kochetkov.



        Comment

        Working...