gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:09:30 25 4
gpt4 key购买 nike

考虑以下代码:

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();
}

代码无法编译!任何的想法?有什么方法可以重新定义在本地声明的基类的纯虚拟析构函数?

最佳答案

cppreference

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com