Re: virtual destructor 101
On 10/18/2010 11:25 PM, Kenneth 'Bessarion' Boyd wrote:
On Oct 18, 12:42 am, Dragan Milenkovic<dra...@plusplus.rs> wrote:
nor can I design a function that returns a raw pointer and give
ownership to the user (actually I can do this, but the user cannot
call "delete" so it doesn't work).
Oops, prior reply didn't notice C++0X context. Still, the clause
requiring access control is unchanged between C++98 and C++0X (it's
just a different paragraph number) so it's clear that, in practice, a
protected virtual destructor does not prohibit using the global delete
operator on a new'ed pointer.
Evidently, it's a protected delete operator that incurs the above
problem. [I haven't ever needed to do this.]
Doesn't matter which standard. Here is on GCC-4.4
class Stack {
protected:
virtual ~Stack() = 0;
public:
virtual void foo() = 0;
};
Stack * create_stack();
void delete_stack(Stack *); // should call this one!
int main() {
Stack * stack = create_stack();
delete stack; // ERROR! virtual ~Stack is protected
}
Which is by the standard. And it works just fine to support
encapsulating memory management for Stack into factory functions.
But I still feel that this is a wrong place for making memory
management contracts.
Yet, no one here to support my claim and to help
expand this discussion beyond my annoying repeating
of "I feel this is wrong, I feel this is wrong!".
--
Dragan
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]