C++子类访问

发布于 2024-12-03 19:28:48 字数 753 浏览 1 评论 0原文

我有一个由许多子类继承的主类。继承的主类至少必须在继承中受到保护,以防止非派生类通过子类使用或更改它。

有没有办法允许各个子类更改彼此继承的主类变量,但不允许公共访问主类?如果不使用friend关键字,这会产生复杂的代码。

在完整的上下文中:

我有一个节点类,可以添加/删除相对于它的节点。有一个依赖于节点类的列表类(和子类),这意味着该节点无法公开访问,以防它也破坏类列表。节点还必须可访问才能列出帮助程序类。

为了确保这种情况发生,我在另一个类 accessnode 中实现了受保护的节点。所有需要节点权限的类都会继承 accessnode 作为受保护的(因此 accessnode 类不是公共的)。这意味着助手和列表类/子类都可以访问节点。

问题是,为了让 TemplateList 通过只读方式复制 CharList(TemplateList 的子类),它需要访问 CharList 的节点(以避免使用 CharList 的迭代器) - 问题是,节点受到保护(以防止外部,非访问节点干扰),并且实现授予节点访问权限的公共方法会破坏这一点。

我需要的是横向继承,因此 accessnode 类型的所有子类都可以访问彼此的节点,而无需公开授予访问权限。

简而言之:

AccessNode 内的(受保护)节点。
TemplateList:受保护的AccessNode。
CharList:受保护的访问节点。
TemplateList需要访问CharList的AccessNode。
AccessNode/Node 不能是公共的。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不回头走下去 2024-12-10 19:28:48

免责声明:这与这个特定问题无关,而是更多地涉及导致您提出这个问题和今天其他问题的一般问题。

我认为你在这里对着错误的树吠叫。我感觉您提供了对列表内部节点的访问,然后期望节点类型本身保护列表免受粗心修改(即那些可能破坏列表不变量的修改)。这样做时,您正在寻求一个复杂的解决方案来解决一个更简单的问题:首先不要让用户访问该节点。

如果您查看 STL 提供的有关容器(特别是列表)的方法,事情就会变得简单得多。该列表是根据一些未知的不可访问节点来实现的。这些节点操作上的访问说明符根本不重要,因为用户无法访问对象本身,因此它们可以是公共的。用户通过不同的代理(iteratorconst_iterator 类型)访问列表的内容,该代理仅提供那些不会扰乱列表状态的操作。

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.

凝望流年 2024-12-10 19:28:48

我不完全确定我理解“子类改变彼此继承的主类变量”的意思。

如果您只想允许派生类访问基类成员变量,则将成员变量设置为受保护。和/或添加一个protected访问器函数。

如果您希望派生类的不同实例修改基类中保存的共享数据,那么您可以向基类添加一个static protected成员变量。所有实例将共享相同的成员变量。

如果您澄清问题将会有所帮助。

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.

嘴硬脾气大 2024-12-10 19:28:48

您始终可以在顶级类中添加受保护的访问器函数,但与其这样做,不如重新考虑设计。

编辑:具体示例:

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 &呵呵,,

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.,

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文