Delete C++ Objects?
تاريخ التسجيل: 2004-10-29 مشاركات: 85
الجامعة: البعث الكلية: الهندسة المعلوماتية المرحلة: السنة الثالثة
|
Hello all,
I just have a simple questions in C++ about deleting objects via base-class pointers.
Assume we have the following code:
class Base {};
class Derived : public Base {
int x;
public:
static void foo() {
Base *b = new Derived;
delete b;
}
};
Does the delete operation free whole the space that was allocated for an instance of class Derived or not?
Thank you.
النور يشع وسط الظلام والظلام لا يستطيع أن يمحوه
|
| |
دخول أو تسجيل لإرسال التعليقات | قراءة: 189 |
تاريخ التسجيل: 2006-11-25 مشاركات: 132
الجامعة: غير ذلك الكلية: الهندسة المعلوماتية الاختصاص: هندسة برمجيات
|
نعم
لأنه عندما نحذف المؤشر, فإن فراغ الذاكرة الذي كان يؤشر عليه المؤشر سيصبح بالنسبة للمعالج فضاء فارغ, و سيعيد الكتاية عليه عندما يحتاجه.
عحد علمي الملفات المحذوفة من الهارد بالأساس ما منصفر موقعا فيه, بس بنحذف المؤشر عهالملفات و المعالج بيعتبرا بعدين فضاءات فارغة.
على كل حال راجع الفصل السابع(7-6) في كتاب كيف تبرمج بلغة c++ للدكتور الدوه جي
و في السماء رزقكم و ما توعدون {} فو رب السماء و الأرض إنه لحق مثل ما أنكم تنطقون
|
| |
دخول أو تسجيل لإرسال التعليقات |
تاريخ التسجيل: 2006-02-08 مشاركات: 92
الجامعة: دمشق الكلية: الهندسة المعلوماتية المرحلة: متخرج الاختصاص: ذكاء صنعي
|
I got your point
Yes it is deleted because
'delete' doesn't look at the pointer type it goes to the real type of the object
Actually , 'delete' is completely like a virtual method when it is invoked it knows the correct version to call
اعمل لدنياك كأنك تعيش أبدا, واعمل لآخرتك كأنك تموت غدا
|
| |
دخول أو تسجيل لإرسال التعليقات |
تاريخ التسجيل: 2004-10-29 مشاركات: 85
الجامعة: البعث الكلية: الهندسة المعلوماتية المرحلة: السنة الثالثة
|
اقتباس:
'delete' doesn't look at the pointer type it goes to the real type of the object
Actually , 'delete' is completely like a virtual method when it is invoked it knows the correct version to call
Sorry but not all objects know their actual types, i.e. not all objects are polymorphic nor have a pointer to their classes.
I know what i did write, my example generates non polymorphic types.
Thanks Ammar_N anyway.
النور يشع وسط الظلام والظلام لا يستطيع أن يمحوه
|
| |
دخول أو تسجيل لإرسال التعليقات |
تاريخ التسجيل: 2004-10-29 مشاركات: 85
الجامعة: البعث الكلية: الهندسة المعلوماتية المرحلة: السنة الثالثة
|
Sorry Helaly but your answer is so far.
My question exatly is how the system knows the size of that object to mark as free in the case of my example????
thanks anyway.
النور يشع وسط الظلام والظلام لا يستطيع أن يمحوه
|
| |
دخول أو تسجيل لإرسال التعليقات |
تاريخ التسجيل: 2006-02-08 مشاركات: 92
الجامعة: دمشق الكلية: الهندسة المعلوماتية المرحلة: متخرج الاختصاص: ذكاء صنعي
|
كتب arbash: اقتباس:
'delete' doesn't look at the pointer type it goes to the real type of the object
Actually , 'delete' is completely like a virtual method when it is invoked it knows the correct version to call
Sorry but not all objects know their actual types, i.e. not all objects are polymorphic nor have a pointer to their classes.
I know what i did write, my example generates non polymorphic types.
Thanks Ammar_N anyway.
O.k
You are completely correct
If your type is not polymorphic you will get your program undefined
The actual objects size will not be determined and will not be deleted
but the problem is in your pattern
C++ programmers usually use "virtual destructors" to solve this problem
This is a C++ pattern
so Add a virtual destructor to the base class and every thing is O.k
اعمل لدنياك كأنك تعيش أبدا, واعمل لآخرتك كأنك تموت غدا
|
| |
دخول أو تسجيل لإرسال التعليقات |
|
مدير
تاريخ التسجيل: 2005-07-15 مشاركات: 2943
الجامعة: دمشق الكلية: الهندسة المعلوماتية المرحلة: السنة الخامسة الاختصاص: هندسة برمجيات
|
كتب Ammar_N:
This is a C++ pattern
so Add a virtual destructor to the base class and every thing is O.k
Yeah
delete isn't the only thing that might go wrong if the type isn't polymorphic, and they always tell you " add a virtual destructor" BUT what if the code was generated or in an external library that you shouldn't touch?!
Well I have a solution for you use C#
|
| |
دخول أو تسجيل لإرسال التعليقات |
تاريخ التسجيل: 2004-10-29 مشاركات: 85
الجامعة: البعث الكلية: الهندسة المعلوماتية المرحلة: السنة الثالثة
|
اقتباس:
If your type is not polymorphic you will get your program undefined
That is the very answer I want.
Thank You.
النور يشع وسط الظلام والظلام لا يستطيع أن يمحوه
|
| |
دخول أو تسجيل لإرسال التعليقات |
|