相关疑难解决方法(0)

覆盖派生类中的成员字段

我有一个代码片段如下:

#include <iostream>

using namespace std;

class Base {
public:
    Base() : b(0) {}
    int get();
    virtual void sayhello() { cout << "Hello from Base with b: " << b << endl; }
private:
    int b;
};

int Base::get() {sayhello(); return b;} 

class Derived : public Base {
public:
    Derived(double b_):b(b_){}
    void sayhello() { cout << "Hello from Derived with b: " << b << endl; }
private:
    double b;
};

int main() {
    Derived d(10.0);
    Base b = d; …
Run Code Online (Sandbox Code Playgroud)

c++ c++11

10
推荐指数
2
解决办法
3万
查看次数

标签 统计

c++ ×1

c++11 ×1