Deleting object with private destructor

前端 未结 3 1961
后悔当初
后悔当初 2021-01-05 01:24

How is that possible that it is allowed to delete object with private destructor in the following code? I\'ve reduced real program to the following sample, but it still comp

3条回答
  •  心在旅途
    2021-01-05 02:13

    Because SomeClass type is not completely declared when invoking operator delete.

    Deleting such a pointer is undefined behavior, but in practice most compilers would just free the memory (if the pointer was non-NULL) and not call the destructor.

    For example, g++ will give you a warning about this issue:

    foo.cpp: In function 'int main(int, char**)':
    foo.cpp:6: warning: possible problem detected in invocation of delete operator:
    foo.cpp:5: warning: 'boo' has incomplete type
    foo.cpp:1: warning: forward declaration of 'struct SomeClass'
    foo.cpp:6: note: neither the destructor nor the class-specific operator delete will be called, even if they are declared when the class is defined.
    

提交回复
热议问题