Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Explicit ctor/dtor calls

Reply
Thread Tools

Explicit ctor/dtor calls

 
 
Jacques Labuschagne
Guest
Posts: n/a
 
      07-20-2003
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

 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      07-20-2003

"Jacques Labuschagne" <(E-Mail Removed)> wrote in message
news:1840039.ZJxocY03RF@klesk...
> 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);
> }
>


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

john


 
Reply With Quote
 
 
 
 
Janusz Szpilewski
Guest
Posts: n/a
 
      07-20-2003
Jacques Labuschagne wrote:
> 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);
> }
>


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.

 
Reply With Quote
 
Michael Kochetkov
Guest
Posts: n/a
 
      07-20-2003

"Jacques Labuschagne" <(E-Mail Removed)> wrote in message
news:1840039.ZJxocY03RF@klesk...
> 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);
> }

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.



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Explicit instantiation of STL vector demands explicit instantiation of all the templates it using internally. krunalbauskar@gmail.com C++ 1 12-25-2006 03:51 PM
What's the difference betwwen explicit instantiaion and explicit specialization? Andy C++ 5 01-30-2005 11:46 PM
Potential improvement on delegation via explicit calls and super Robert Dick Python 1 12-17-2004 02:48 PM
Is explicit template qualification required for explicit delete? J.T. Conklin C++ 1 08-11-2004 02:06 AM
Explicit calls to unexpected()? Ken C++ 1 05-26-2004 08:01 AM



Advertisments