C++ 是什么意思?在类中使用平均值?
在类定义中使用 using 意味着什么?
class myClass {
public:
[...]
using anotherClass::method;
};
What does it mean to have a using inside a class definition?
class myClass {
public:
[...]
using anotherClass::method;
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
该声明取消隐藏基类成员。这最常用于允许成员函数的重载。例子:
That declaration unhides a base class member. This is most often used to allow overloads of a member function. Example:
我见过的情况:
因为我在 B 中实现了一个具有不同签名的新 foo 函数,所以它隐藏了 A 中的 foo 函数。为了覆盖此行为,我会这样做:
The case I've seen it:
Because I have implemented a new foo function in B with a different signature it hides the foo functions from A. In order to override this behavior I would do:
大多数情况下,像这样的语法的使用方式如下:
这里的
using
声明取消隐藏来自父类的成员声明。如果衍生
中的另一个成员声明可能会隐藏基
中的成员,有时这是必要的。Most often, syntax like this is used like so:
The
using
declaration here unhides a member declaration from the parent class. This is sometimes necessary if another member declaration inderived
may hide the member frombase
.如果 anotherClass 是包含类似成员函数的基类
,并且您决定重载派生类中的函数,就像
它“隐藏”基类中的 f() 一样。例如,通过指向派生类的指针调用 f() 会导致错误,因为编译器不会“看到”不再从基类获取参数的 f() 版本。
通过编写,
您可以将基类函数带回作用域,从而启用重载解析,就像您最初期望的那样。
If anotherClass is a base class that contains a member function like
and you decide to overload the function in the derived class like
it "hides" f() in the base class. Calling f() through a pointer to the derived class for example, would result in an error, since the compiler does not "see" the version of f() taking no arguments from the base class anymore.
By writing
you can bring the base classes function back into scope, thus enabling overload resolution as you might have expected it to work in the first place.