用于在C ++中访问受保护虚拟功能的Friend Class

时间:2016-11-02 05:56:52

标签: c++ inheritance protected virtual-functions friend-class

我有三个课程:Class AClass BClass C。来自Class B的{​​{1}}为derivedClass A中声明的protectedvirtual function class AClass B中定义。 C类是A类的friend函数。现在我需要从C类调用virtual function。当我尝试编译时,我得到的错误为cannot access protected member declared in class A

不可能吗?

编辑:已添加代码:

//A.h

class A
{
    friend class C;
 protected:
   virtual int SetPtr(unsigned long long Offset)=0;
};



//B.h
class B: public A
{
protected:
            int SetPtr(unsigned long long Offset) override;//function defined in B.cpp
};

//C.h

class C
{
public:
    C(A& aObj);
    int CallMyFun();
private:
    A& obj;
};

//C.cpp:

C::C(A& aObj) :obj(aObj)
{
}

int C::CallMyFun()
{
    obj.SetPtr(1);
    return 0;
}

0 个答案:

没有答案