写错是不对的:
class A {
public:
virtual ~A() = 0;
};
Run Code Online (Sandbox Code Playgroud)
对于抽象基类?
至少在MSVC中编译......它会在运行时崩溃吗?
我正在学习C++,我无法理解接口的工作原理.我设置了一个不起作用的小例子,我不明白我做错了什么.
我已经阅读了这个答案,这有助于我宣布界面,但(即使我仍然不完全理解声明)我现在关注的更多是关于用法.我也读过这个答案,但是,我仍然无法使用我的界面,请看这个例子:
shape.h
#ifndef SHAPE_H
#define SHAPE_H
class Shape
{
public:
virtual ~Shape(){}
virtual int getArea() = 0;
};
#endif // SHAPE_H
Run Code Online (Sandbox Code Playgroud)
rectangle.h
#ifndef RECTANGLE_H
#define RECTANGLE_H
#include "shape.h"
class Rectangle : public Shape
{
public:
Rectangle(){}
Rectangle(int w, int h) : width{w}, height{h} {}
virtual int getArea();
private:
int width = 0;
int height = 0;
};
#endif // RECTANGLE_H
Run Code Online (Sandbox Code Playgroud)
rectangle.cpp
#include "rectangle.h"
int Rectangle::getArea()
{
return width * height;
}
Run Code Online (Sandbox Code Playgroud)
weird.h
#ifndef WEIRD_H
#define …Run Code Online (Sandbox Code Playgroud)