更改方法的保护级别是否被视为良好实践?

发布于 2024-12-07 05:18:01 字数 271 浏览 0 评论 0原文

换句话说,如果我有一堂课

class A
{
public:
   A() { .. }
   virtual void somemethod() { .. }
};

可以写吗

class B : public A
{
public:
   B() { .. }
protected:
   virtual void somemethod() { .. }
};

?或者这种方法有一些缺点吗?

In other words if I have a class

class A
{
public:
   A() { .. }
   virtual void somemethod() { .. }
};

is it ok to write

class B : public A
{
public:
   B() { .. }
protected:
   virtual void somemethod() { .. }
};

or are there some drawbacks with this approach?

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

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

发布评论

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

评论(4

苦行僧 2024-12-14 05:18:01

这种方法的主要缺点是,人们总是可以获取 A 的指针/引用并调用公共的somemethod。你为什么要做这样的事?如果 BA,并且 A 有一个 public somemethod 那么做B

The main drawback with that approach is that one can always take a pointer/reference to A and invoke somemethod where is public. Why would you want to do such thing? If B is an A, and As have a public somemethod then so do Bs.

寂寞美少年 2024-12-14 05:18:01

我想说这违背了多态性的目的,因为当你编写一个接受多态类型的函数时,派生类型应该与它同样工作:

void fun(A* a){
   a->somemethod();
}
...
A* a = new B();
fun(a); // Shouldn't this work?!
        // According to Liskov Principle, you are doing it wrong!
        // but really who cares, it depends on your justification
        // of a solution to the the problem at hand.

恕我直言,这取决于你试图解决的具体问题,因为我不知道相信“永远”成功的“最佳实践”。

I would say this defeats the purpose of polymorphism, because when you write a function that accepts a polymorphic type, the derived type should work equally well with it:

void fun(A* a){
   a->somemethod();
}
...
A* a = new B();
fun(a); // Shouldn't this work?!
        // According to Liskov Principle, you are doing it wrong!
        // but really who cares, it depends on your justification
        // of a solution to the the problem at hand.

IMHO, it depends on the specific problem you are trying to solve because I don't believe in "always" successful "best practice".

北方的韩爷 2024-12-14 05:18:01

这种方法没有缺点。唯一的限制B::somemethod()不能用B对象/指针/引用调用。现在只能使用 A 的指针或引用来调用它。

事实上,我发现有时这种限制是故意引入的。这种情况是当 B 类 的开发人员想要传达这样的消息时,somemethod() 只能使用基类仅多态调用处理。

There is no drawback with this approach. The only limitation is that B::somemethod() cannot be called with B object/pointer/reference. It can be now invoked only using A's pointer or reference.

In fact, I have seen that sometimes this limitation is introduced intentionally. Such scenarios are when the developer of class B wants to convey the message that, somemethod() is meant to be called only polymorphically using base class handle.

岁月苍老的讽刺 2024-12-14 05:18:01

没有缺点。

但这样做并没有真正的优势。
您仍然可以通过指向案例类的指针访问 somemethod()

所以在技术方面没有缺点也没有优势。
现在我们来看看使用对象是多么容易。这里有一个缺点,因为您会让对象的用户感到困惑(为什么它在此级别受到保护,而不是较低级别)?因此,您正在为自己创造工作,记录您做出此决定的原因以及您试图通过使用此技术实现的目标。

你真正想要实现的目标是什么?

No drawback.

But no real advantage to do this.
You still have accesses to somemethod() via a pointer to the case class.

So no drawback and no advantages on the technical side.
So now we move onto how easy it is to use you object. Here there is a downside as you are confusing the user of the object (why is it protected at this level but not the lower level)? So you are creating work for yourself in documenting why you made this decision and what you are trying to achieve by using this technique.

What is it that you really trying to achieve?

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