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.