开发者

C++ subclass access

I have 开发者_运维知识库a main class that is inherited by numerous subclasses. The inherited main class has to be at least protected in inheritance to prevent non-derivative classes from using or altering it via the subclasses.

Is there a way to permit the various subclasses to alter each other's inherited main class variables, but without permitting public access to the main class? And without using the friend keyword given this would produce complicated code.

In full context:

I have a node class that add/remove nodes relative to it. There is a list class (and subclasses) that rely upon the node class, which means the node cannot be publicly accessible in-case it also breaks the class list. Node has to also be accessible to list helper classes.

To ensure that occurs, I implemented node under protected inside another class, accessnode. All classes wanting rights to node inherit accessnode as protected (so the accessnode class isn't public). This means the helper and the list class/subclasses all gain access to node.

The problem is, in order for TemplateList to copy CharList (a subclass of TemplateList) via read-only, it needs access to the nodes of CharList (to avoid using CharList's iterator) - the problem is, the nodes are protected (to prevent external, non-accessnode interference), and implementing a public method that grants access to the nodes would defeat the point.

What I need is sideways inheritance, so all subclasses of type accessnode can access each other's node without granting access publicly.

In short:

(Protected)Node inside AccessNode.

TemplateList : Protected AccessNode.

CharList : Protected AccessNode.

TemplateList needs to access CharList's AccessNode.

AccessNode/Node cannot be public.


Disclaimer: This is quite unrelated to this particular question, but more on the general problem that lead you to this and the other questions from today.

I think that you are barking at the wrong tree here. I get the feeling that you provide access to your list's internal nodes, and then expect that the node type itself protects the list from careless modifications (i.e. those that could break the invariants of the list). In doing so, you are pursuing a complex solution to a much simpler problem: do not let users access the node in the first place.

Things become much simpler if you look at the approach provided by the STL regarding containers and in particular lists. The list is implemented in terms of some unknown innaccessible nodes. The access specifiers on the operations of those nodes don't matter at all, since users cannot gain access to the object itself, so they can be public. Users gain access to the contents of the list through a different proxy (iterator, const_iterator types) that provides only those operations that cannot mess the state of the list.


I'm not completely sure I understand what you mean by "subclasses [to] alter each other's inherited main class variables".

If you want to allow access to a base class member variable by derived classes only then make the member variable protected. And/or add a protected accessor function.

If you want different instances of the derived classes to modify shared data held in the base class then you could add a static protected member variable to the base class. All instances would share the same member variable.

It would help if you clarified the problem.


You can always just add a protected accessor function in the top level class, but rather than do that it would probably be much better to rethink the design.

EDIT: concrete example:

class Base
{
protected:
    struct State
    {
        int     m1;
        char    m2;

        State(): m1(), m2() {}
    };

    State   state_;

    static State& state( Base& o) { return o.state_; }
};

class Derived
    : public Base
{
public:
    void foo( Base& other )
    {
        Base::State&    baseState   = state( other );
        // Blah blah.
    }
};

int main()
{
    Derived o;
    // Blah blah.
}

Cheers & hth.,

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜