引用接口赋值

继承接口赋值的时候,需要采用引用的方式,需要在初始化列表里面初始化

 

class Parent {
public:
    virtual void ShowEntry() { cout << " entry parent "<< endl; };
};
class Child : public Parent {
    virtual void ShowEntry() override { cout << " entry child "<< endl; };
};

class People {
private:
    Parent& pl; //定义需要是引用
public:
    People(Parent& _input): pl(_input) {}; // 初始化列表里面初始化
    void Show() { pl.ShowEntry(); };
};
TEST(override, bridge1)
{
    Child cld;
    People p(cld);
    p.Show();
}

 

posted on 2022-05-16 20:16  蜀山菜鸟  阅读(35)  评论(0)    收藏  举报