本地抽象类的纯虚拟析构函数

MEM*_*EMS 4 c++ inheritance virtual-functions

考虑以下代码:

struct  A {
    virtual void foo() {}
    virtual ~A() = 0;
};
struct B :public A
{
    virtual void foo() {};
};
A::~A() {}; 
int main()
{
    A * a = new B();
    a->foo();
}
Run Code Online (Sandbox Code Playgroud)

它工作得很好.但现在考虑我们需要在函数内部声明我们的类的第二个代码:

void foo()
{
    struct  A {
        virtual void foo() {}
        virtual ~A() = 0;
    };
    struct B :public A
    {
        virtual void foo() {};
    };
    A::~A() {}; //error C2352 : 'A::~A' : illegal call of non - static member function

    A * a = new B();
    a->foo();
}
int main()
{
    foo();  
}
Run Code Online (Sandbox Code Playgroud)

代码不能编译!任何的想法?是重新定义在本地声明的基类的纯虚析构函数的任何方法吗?

A.S*_*S.H 6

cppreference

类声明可以出现在(...)和函数体内,在这种情况下它定义了一个本地类

...

必须在类体内完全定义本地类的成员函数