[英]Why using cannot be used to define a virtual function?
I've recently discovered the use of using
to import a base class function into the namespace of a derived class (when it is being hidden). 我最近发现了使用
using
将基类函数导入到派生类的名称空间中(隐藏时)。 I was trying to use it to import a function from a base class as an implementation of the function in a derived class: 我试图用它从基类中导入一个函数,作为派生类中函数的实现:
class A {
public:
virtual void foo() = 0;
};
class B {
public:
void foo() {
}
};
class C : public A, public B {
public:
using B::foo;
};
int main()
{
C c;
}
This won't compile as A::foo()
is a pure virtual function in C
. 由于
A::foo()
是C
的纯虚函数,因此不会编译。 I was hoping that using B::foo;
我希望
using B::foo;
would make an implementation of foo()
. 将实现
foo()
。 Why isn't it so ? 为什么不这样呢?
You have two different functions: A::foo()
and B::foo()
. 您有两个不同的函数:
A::foo()
和B::foo()
。 Although they have the same unqualified name, they are not related. 尽管它们具有相同的不合格名称,但它们并不相关。
B::foo()
does not and cannot override A::foo()
because B
is not a subclass of A
. B::foo()
不会并且不能覆盖A::foo()
因为B
不是A
的子类。
C
inherits both functions from A
and B
. C
继承了A
和B
两个函数。 You use public inheritance for both base classes, so A::foo()
and B::foo()
are already visible in C
(note that you need qualified name to invoke the functions to avoid ambiguity). 您对两个基类都使用了公共继承,因此
A::foo()
和B::foo()
在C
中已经可见 (请注意,您需要限定名称来调用函数,以避免产生歧义)。 So your using declaration has actually no effect. 因此,您的using声明实际上没有任何作用。
A using declaration, when used inside a class, has to do with overloading , but it has nothing to do with overriding . 在类中使用using声明时,它与重载有关 ,但与overrideing无关。 These are very different concepts .
这些是非常不同的概念 。
Overloading is about having different functions with the same name but different argument sets. 重载涉及具有相同名称但参数集不同的不同函数。
Overriding is about polymorphism, ie having varying implementations of a base-class method in derived classes. 覆盖是关于多态的,即在派生类中具有不同的基类方法实现。
A using declaration introduces a function name from a base class into a derived class, where that name would be hidden by another function with the same name but different arguments . using声明将函数名称从基类引入派生类,在该派生类中,该名称将被具有相同名称但参数不同的另一个函数隐藏。 For example:
例如:
class X
{
public:
void func() {}
};
class Y : public X
{
public:
void func(int arg) {}
};
Y::func
does not override X::func
, since its arguments are different. Y::func
不会覆盖 X::func
,因为其参数不同。 Moreover, it also hides the name func
from the base class, so it can be called only via qualified name, eg: 而且,它还从基类中隐藏了名称
func
,因此只能通过限定名称来调用它,例如:
X x;
x.func(); // ok
Y y;
y.func(1); // ok, Y::func called
y.func(); // error, base-class name func is hidden by local name
y.X::func(); // ok, qualified name, X::func called
In this case, a using declaration would introduce the name from the base class into the derived class, making func
callable without name-qualifying: 在这种情况下,using声明会将名称从基类引入到派生类中,从而使
func
没有名称限定的情况下被调用:
class Y : public X
{
public:
using X::func;
void func(int arg) {}
};
// ...
Y y;
y.func(); // ok, X::func called
using
in C++ has a different meaning and designates that you would like to be able to access a function/object in another namespace without typing the namespace name explicitly. 在C ++中
using
具有不同的含义,它表示您希望能够在不显式键入名称空间名称的情况下访问另一个名称空间中的函数/对象。 It has nothing to do with overriding. 它与覆盖无关。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.