这是对班级友谊的恰当运用吗?

发布于 2024-10-14 05:38:07 字数 63 浏览 3 评论 0原文

创建 Windows 父类和子对话框类时,通常最好让子类成为父类的友元来访问其私有数据,还是应该使用访问器函数?

When creating windows parent and child dialog classes, is it generally a good idea to make the child class a friend of parent class to access its private data or should you use accessor functions?

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

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

发布评论

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

评论(3

茶底世界 2024-10-21 05:38:07

对友元的需求很少——通常是当您需要在一个类中重新实现某些深层行为而不重写它以便它们都从单个基类继承或不提供大量访问者时。

我唯一需要它的时候是在 ActiveX 中重写基于 openGL 的渲染器 - 当我需要获取大量低级模型数据,但无法(出于非技术原因)重新实现常见的 ABC 时。

The need for friend is rare - generally it's when you need to reimplement some deep behaviour in one class without either rewriting it so that they both inherit from a single base or without providing lots of asccessors.

Only time I have needed it was to rewrite an openGL based renderer in ActiveX - when I needed to get at a lot of the low level model data, but couldn't (for non-technical reasons) reimplement a common ABC.

债姬 2024-10-21 05:38:07

我最近遇到了类似的情况,我想将 A 类的一些私有成员变量公开给 B 类。

我不想添加公共访问器函数,因为这会将这些成员公开给所有其他类。

我不想让 B 成为 A 的朋友,因为这会将 A 的所有私有成员暴露给 B。

所以我专门为了这个目的创建了另一个类(A 到 B 私有接口)。它是 A 的友元,除了访问器函数之外什么都没有:

class A
{
    int top_secret; // only A has access to it
    int secret; // only A and B have access to it
    friend struct AToBInterface;
};

struct AToBInterface
{
    static int secret(const A& object) {return object.secret;}
};

class B
{
    void DoSecretStuff(A& object)
    {
        int secret = AToBInterface::secret(object);
        ...
    }
};

您可以调整语法(例如,如果您需要读写访问),这只是一个想法。我只在代码中的一处使用它,所以如果语法有点复杂也没有问题。

I had a similar situation recently, where i wanted to expose a few private member variables of class A to class B.

I didn't want to add public accessor functions because this would expose these members to all other classes.

I didn't want to make B a friend of A because this would expose all private members of A to B.

So i made another class (A-to-B private interface) solely for that purpose. It is a friend of A, and it has nothing except the accessor functions:

class A
{
    int top_secret; // only A has access to it
    int secret; // only A and B have access to it
    friend struct AToBInterface;
};

struct AToBInterface
{
    static int secret(const A& object) {return object.secret;}
};

class B
{
    void DoSecretStuff(A& object)
    {
        int secret = AToBInterface::secret(object);
        ...
    }
};

You can tweak syntax (e.g. if you need read-write access), it's just an idea. I use it in only one place in code, so no problem if syntax is a bit hairy.

怀里藏娇 2024-10-21 05:38:07

恕我直言,几乎从来没有。

“friend”通常用于破坏封装,因为它可以允许外部实体访问类的私有数据。你几乎不想这样做——通过公共访问器(可以检查有效性)公开“半私有”数据通常比向另一个类公开私有信息(这可能会压倒你)更好/更安全。

然而,有时您会拥有一对/一组非常密切相关的类,将它们保留为单独的类是有意义的,但它们需要对实际上不应该与整个世界共享的数据进行低级别访问。这就是可以使用“朋友”的地方——要小心。

一般来说,尝试限制友元的范围(例如友元方法而不是友元类),以尽量减少允许直接访问私有数据的区域。保持尽可能简单 - 请记住,阅读您代码的另一个程序员可能会认为“私有”意味着数据确实是私有的,并且他们可能会被朋友绊倒。此外,您使用的朋友越多,您的设计就越紧密耦合且难以维护。它们可能很有用,但请确保每次使用都有充分的理由。

IMHO, almost never.

"friend" often tends to be used to break encapsulation in that it can allow an external entity to access the private data of your class. You almost never want to do this - it is often better/safer to expose "semi-private" data via public accessors (that can check validity) than to expose private information to another class (which can tromp all over you).

However, sometimes you will have a pair/group of very closely related classes where it makes sense to keep them as separate classes, but they need low level access to data that really shouldn't be shared with the world at large. This is where 'friend' can be used - with care.

Generally, try to restrict the scope of friends (e.g. friend methods rather than friend classes) to minimise the areas where direct access to private data is allowed. Keep it as simple as possible - remember that another programmer reading your code might think "private" means data is truly private, and they could be tripped up by friends. Also, the more friends you use, the more tightly-coupled and hard to maintain your design will tend to be. They can be useful, but make sure you have good justification for each use.

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