RE: [Dev-C++] Constructor and Destructor
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Hossein H. <ult...@em...> - 2003-07-10 18:36:49
|
Dear all,
After all, I found the following lines in the Standard draft, which I
cite directly: (Section 3.8, Case 5)
"Before the lifetime of an object has started but after the storage
which the object will occupy has been allocated or, after the
lifetime of an object has ended and before the storage which the
object occupied is reused or released, any pointer that referes to
the storage location where the object will be or was located in may
be used but only in limited ways. Such a pointer referes to allocated
storage, and using the pointer as if the pointer were of type void*
is well-defined. Such a pointer may be dereferenced but the resulting
lvalue may only be used in limited ways, as described below. If the
object will be or was of a class type with a nontrivial destructor,
and the pointer is used as the operand of delete-expresion, the
program has undefined behavior. If the object will be or was a
non-POD class type, the program has undefined behavior if:
1) the pointer is used to access a non-static data member or call a
non-static member function of the object, or
2) the pointer is implicitly converted to a pointer to a base class
type, or
3) the pointer is used as the operand of a static_cast (except when
the convesion is to void*, or to void* subsequently to char* or
unsigned char*).
4) the pointer is used as the operand of a dynamic_cast.
Example:
struct B
{
virtual void f();
void mutate();
virtual ~B();
};
struct D1: B { void f();};
struct D2: B { void f();};
void B::mutate() {
new (this) D2; //reuse storage - ends the lifetime of *this
f(); //undefined behavoir
... = this; //OK. this points to valid memory
}
void g() {
void* p = malloc(sizeof(D1) + sizeof(D2));
B* pb = new (p) D1;
pb->mutate();
&pb; //OK: pb points to valid memory
void* q = pb; //OK: pb points to valid memory
pb->f(); //undefined behavior, lifetime of *pb has ended
}"
(Gush! The pdf of Standard I have doesn't support copy/pasting so I
wrote the whole by periodically jumping from this window to that one
and vice versa.)
I hope this to be clear enough. As now, I myself, can understand
everything about that troublesome programme (which was quite handsome
as well)!
Regards,
--Hossein
--
__________________________________________________________
Sign-up for your own FREE Personalized E-mail at Mail.com
http://www.mail.com/?sr=signup
CareerBuilder.com has over 400,000 jobs. Be smarter about your job search
http://corp.mail.com/careers
|