相关疑难解决方法(0)

构造函数的C++虚函数

为什么以下示例打印"0"以及必须更改它以打印"1",如我所料?

#include <iostream>
struct base {
   virtual const int value() const {
      return 0;
   }
   base() {
      std::cout << value() << std::endl;
   }
   virtual ~base() {}
};

struct derived : public base {
   virtual const int value() const {
      return 1;
   }
};

int main(void) {
   derived example;
}
Run Code Online (Sandbox Code Playgroud)

c++ oop virtual constructor class

57
推荐指数
3
解决办法
4万
查看次数

标签 统计

c++ ×1

class ×1

constructor ×1

oop ×1

virtual ×1