简体   繁体   English

使从一个基类派生的类能够使用继承的受保护成员

[英]Make classes derived from one base class able to use inherited protected members

I created a base class with one protected method returning the id of the object.我创建了一个基类,其中一个受保护的方法返回对象的 id。 I want only derived classes to be able to query this id on other derived classes, but hide it from classes outside the inheritance hierarchy.我只希望派生类能够在其他派生类上查询此 id,但对继承层次结构之外的类隐藏它。

class Identifiable {
    public:
        virtual ~Identifiable() = default;
    protected:
        virtual auto getId() const noexcept -> unsigned = 0;
    };

    class ObjectA: public Identifiable {
    protected:
        auto getId() const noexcept -> unsigned override { return 0; }
    };

    class SpecificObjectA: public ObjectA {
    protected:
        using ObjectA::getId;
    };

    class ObjectB: Identifiable {
    public:
        explicit ObjectB(const SpecificObjectA& objectA) {
            objectA.getId(); // error C2248: 'SpecificObjectA::getId': cannot access protected member declared in class 'SpecificObjectA'
        }
    protected:
        auto getId() const noexcept -> unsigned override { return 0; }
};

Is there any way to make it work other than adding a next method?除了添加下一个方法之外,还有什么方法可以使它工作吗?

auto getId(const Identifiable& identifiable) const noexcept -> unsigned {
    return getId();
}

No, that's not possible.不,那不可能。 Otherwise, it would let you use virtual protected members from any class by creating another class derived from the base class in question.否则,它会让您通过创建从相关基类派生的另一个类来使用来自任何类的虚拟受保护成员。

You cannot do as intended, but you might try this:你不能按预期做,但你可以试试这个:

class Identifiable
{
public:
    virtual ~Identifiable() = default;
protected:
    virtual unsigned getId() const noexcept = 0;
    static unsigned int getId(Identifiable const& other)
    {
        return other.getId();
    }
};

Now you can do:现在你可以这样做:

explicit ObjectB(const SpecificObjectA& objectA)
{
    Identifiable::getId(objectA);
}

I personally wouldn't use trailing return type unless you really need it, otherwise it just reduces readability.我个人不会使用尾随返回类型,除非你真的需要它,否则它只会降低可读性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使基础 class 的方法使用派生 class 集的继承的受保护成员? C++ - How to make method of a base class use inherited protected members that derived class sets? C++ 无法访问派生类中的基类保护成员! (在虚函数中) - Not able to access base protected members in derived class ! ( in virtual functions ) 我可以从派生类中的静态函数访问基类受保护的成员吗? - Can I access a base classes protected members from a static function in a derived class? 从派生类访问基类的受保护数据成员 - Access protected data members of the base class from the derived class 从基类继承私有成员到派生类C ++ - Inheriting private members from base class to derived classes c++ 在派生类中使用虚拟基类中的受保护ctor - use protected ctor from virtual base class in derived class 使用派生类的朋友提供的基类的受保护静态函数 - Use protected static function of the base class from the friend of the derived class 有没有办法让派生的 class 不继承基础 class 的所有成员? - Is there a way to make a derived class not inherit all the members from the base class? 我如何从另一个类的基类访问派生类中的受保护成员 - How can i acces protected members in a derived class from a base class that is firend of another class 从公共派生类访问受保护的成员 - Access protected members from a public derived class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM