|
-
November 21st, 2003, 05:00 PM
#11
Well now that I think about it:
I know that it is safe to directly delete an object that is derived from a class that does not have a virtual destructor. Therefore, make me amend what I've been saying:
Since my example derived from string_vector, and if string_vector has a virtual destructor, and you delete directly a "string_vector" object or pointer, you are fine. This is not disputed, but I wasn't thinking this at the time. Sorry for the wrong information.
Now, the rub is how far up the coder will declare their pointer in the hierarchy: if the coder were to go up one more above the hierarchy and declared a pointer to vector<string> and used it polymorphically, then they would be in trouble.
Code:
int main()
{
std::vector<std::string> *pMyVect;
if ( do_some_customizations )
pMyVect = new MyCustomizedStringVector;
else
pMyVect = new AnotherCustomizedStringVector;
//...
delete pMyVect; // trouble
}
So you still need to post a warning as to how far up the hierarchy someone can use your class polymorphically -- right now, they can stop at string_vector, but go no further up the branch when a pointer is declared.
AvDav's most recent post shows the dangers of deriving from a class that does not have a virtual destructor, and how easily it can be erroneously used in a program without any great programming effort.
Regards,
Paul McKenzie
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|