简体   繁体   中英

How can a protected pure virtual function in the base class be used by a friend class of the base class?

I have an abstract base class

class Base {
friend class Friend;
public:
    virtual ~Base() {}
protected:
    virtual void eval() = 0;

};

class Derived: public Base {
public:
     Derived = default;
protected:
     virtual void eval() { // implementation of eval()}
};

Note that class Friend is the friend of Base, not Derived. My question is: Can Friend call eval() by using the pointer to Base? For example,

void Friend::foo() {
    shared_ptr<Base> bpt(new Derived);
    bpt->eval()
}

Access to class members is determined based on static type, not on dynamic one. Static type of pointer is Base* and Friend got access to eval() in vtable of Base , but call is routed to a member function of dynamic type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM