被覆盖的纯虚函数是虚拟的吗?
Is an overridden pure virtual function, virtual?
下面你会看到 3 类。 我的问题是为什么我可以在 FunnySquare
Class 的 getArea()
上使用 override
,即使它是 未在 Square
Class.
中标记为虚拟
我的假设是一个被覆盖的纯虚函数是虚的,即使它没有被指定,但我不确定这是真的,也找不到任何参考来确认。
class Shape {
public:
virtual float getArea() = 0;
};
class Square : public Shape {
private:
float length;
public:
Square(float len){
length = len;
}
float getArea() override {
return length * length;
}
void sayHi() {cout << "hell0" << endl;}
};
class FunnySquare : public Square {
public:
FunnySquare(float len) : Square(len) {}
void tellJoke() {std::cout << "I gave you the wrong area haha" << endl;}
float getArea() override {
return 2.0;
}
};
是的,Square::getArea
和FunnySquare::getArea
都是virtual
。
(强调我的)
Then this function in the class Derived is also virtual (whether or not the keyword virtual is used in its declaration) and overrides Base::vf (whether or not the word override is used in its declaration).
下面你会看到 3 类。 我的问题是为什么我可以在 FunnySquare
Class 的 getArea()
上使用 override
,即使它是 未在 Square
Class.
我的假设是一个被覆盖的纯虚函数是虚的,即使它没有被指定,但我不确定这是真的,也找不到任何参考来确认。
class Shape {
public:
virtual float getArea() = 0;
};
class Square : public Shape {
private:
float length;
public:
Square(float len){
length = len;
}
float getArea() override {
return length * length;
}
void sayHi() {cout << "hell0" << endl;}
};
class FunnySquare : public Square {
public:
FunnySquare(float len) : Square(len) {}
void tellJoke() {std::cout << "I gave you the wrong area haha" << endl;}
float getArea() override {
return 2.0;
}
};
是的,Square::getArea
和FunnySquare::getArea
都是virtual
。
(强调我的)
Then this function in the class Derived is also virtual (whether or not the keyword virtual is used in its declaration) and overrides Base::vf (whether or not the word override is used in its declaration).