gpt4 book ai didi

c++ - C++ 中虚函数的行为

转载 作者:可可西里 更新时间:2023-11-01 15:36:12 27 4
gpt4 key购买 nike

我有一个问题,下面有两个类:

  class Base{
public:
virtual void toString(); // generic implementation
}

class Derive : public Base{
public:
( virtual ) void toString(); // specific implementation
}

问题是:

  • 如果我想类Derive 的子类使用Base 类型的指针执行多态性,是否需要括号中的关键字virtual?

  • 如果答案是否定的,那么 Derive 类的成员函数 toString 有和没有 virtual 有什么区别?

最佳答案

C++03 §10.3/2:

If a virtual member function vf is declared in a class Base and in a class Derived, derived directly or indirectly from Base, a member function vf with the same name and same parameter list as Base::vf is declared, then Derived::vf is also virtual (whether or not it is so declared) and it overrides Base::vf.

关于c++ - C++ 中虚函数的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2669681/

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