gpt4 book ai didi

c++ - 了解 'protected virtuals' 和 'public virtuals' 之间的比较

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:54:36 27 4
gpt4 key购买 nike

问题来自C++ faq。

http://www.parashift.com/c++-faq-lite/protected-virtuals.html

使用公共(public)重载虚拟的代码:

class Base {
public:
virtual void f(int x); ← may or may not be pure virtual
virtual void f(double x); ← may or may not be pure virtual
};

通过 Public Overloaded Non-Virtuals Call Protected Non-Overloaded Virtuals 习惯用法改进这一点:

class Base {
public:
void f(int x) { f_int(x); } ← non-virtual
void f(double x) { f_dbl(x); } ← non-virtual
protected:
virtual void f_int(int);
virtual void f_dbl(double);
};

作者说:

The idea of the Public Overloaded Non-Virtuals Call Protected Non-Overloaded Virtuals idiom is to change the public overloaded methods to non-virtuals, and make those call protected non-overloaded virtuals.

但是我不明白作者是怎么说这个成语如何降低风险的:

the idiom packs the complexity of properly managing the hiding rule into the base class (singular). This means the derived classes (plural) more-or-less automatically handle the hiding rule, so the various developers who produce those derived classes can remain almost completely focused on the details of the derived classes themselves — they need not concern themselves with the (subtle and often misunderstood) hiding rule. This greatly reduces the chance that the writers of the derived classes will screw up the hiding-rule.

为什么这样可以解决隐藏问题?据我了解,名称隐藏与成员函数是否为“虚拟”无关。如果 'base' 的派生类重写函数 f(),它仍然会隐藏 f(int) 和 f(double),对吗?

从这个成语中我只能看到作者将'base'虚拟f()更改为非虚拟,并将辅助函数f_int(),f_dbl()放在'protected virtual'中,如成语名称所述。它没有做任何好事,但相反消除了从基类指针/引用动态绑定(bind)的可能性。这个成语的真正好处是什么?

更新

Kerrek,你是说这个吗?我不完全理解你的回答的第二段。能举个例子吗?

class base {
public:
virtual void f(int x);
virtual void f(double x);
}

class derived : public base {
public:
virtual int f(int x); // oops, will hide base::f(int x) AND base::f(double x)
}

base *bp = new base();
base *dp = new derived();
bp->f(int i); // ok
dp->f(int i); // surprise!
dp->f(double d); // compile error!


class Base {
public:
void f(int x) { f_int(x); }
void f(double x) { f_dbl(x); }
protected:
virtual void f_int(int);
virtual void f_dbl(double);
};

class derived : public base {
public:
// nothing to override here 'cause f() is non virtual
protected:
// because f_int() and f_dbl are unique names, override or hide f_int() will not affect f_dbl()?
virtual int f_int(int); // oops, will hide base::f(int x), but developer may want this on purpose
// no effect on f_dbl(), which is good
}

base bobj;
derived dobj;
bobj.f(int i); // ok
dobj.f(double d); // ok

最佳答案

如果派生类声明了 void f(int),则它覆盖虚函数,并且隐含了 virtual 说明符。如果派生类声明 int f(int),它会隐藏基函数。我猜你对此很熟悉。

当你想让别人基于你的基类开发代码时,问题就来了。使用天真的方法,每个派生类都必须小心添加正确的覆盖,以免意外隐藏函数并得到一个有效但错误的程序(即用户说 f() 但得到错误的东西)。使用“public non-virtual”惯用语,用户总是 secret 调用 f(),并且库开发人员可以通过覆盖唯一命名的 protected 函数来仅覆盖她感兴趣的那些部分,而无需必须触摸可能影响其他用户的名称。

关于c++ - 了解 'protected virtuals' 和 'public virtuals' 之间的比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11825039/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com