作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
考虑以下代码:
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();
}
它工作得很好。但现在考虑第二个代码,我们需要在一个函数内本地声明我们的类:
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();
}
代码无法编译!任何的想法?有什么方法可以重新定义在本地声明的基类的纯虚拟析构函数?
最佳答案
A class declaration can appear (...) and inside the body of a function, in which case it defines a local class
...
Member functions of a local class have to be defined entirely inside the class body
关于c++ - 本地抽象类的纯虚拟析构函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41977517/
我是一名优秀的程序员,十分优秀!