简体   繁体   English

为什么不能使用using来定义虚拟函数?

[英]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继承了AB两个函数。 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.

相关问题 为什么使用虚拟表来解析函数调用? - Why virtual tables are used to resolve function calls? 那么,为什么我必须在基类中定义虚函数? - So, why do I have to define virtual function in a base class? 为什么不能使用 auto 来定义隐式删除的构造函数 - Why auto cannot be used to define an implicitly deleted constructor 为什么不能使用函数的 typedef 来定义函数? - Why can't a typedef of a function be used to define a function? 为什么在那里使用“虚拟”关键字? - Why is "virtual" keyword used there? 我是C++的新手,想知道为什么c++使用纯虚拟function? 为什么要用纯虚拟function? - I am new in C++, I want to know, why the pure virtual function is used in c++ ?. Why to use pure virtual function? 如果它不纯,会使用虚拟成员函数吗? - A virtual member function is used if it is not pure? 为什么使用存储在虚拟方法表中的地址对虚拟函数的函数调用返回垃圾? - Why is the function call to the virtual function using the address stored in the virtual method table returning garbage? 如果结构化绑定不能是constexpr,为什么它们可以在constexpr函数中使用? - If structured bindings cannot be constexpr why can they be used in constexpr function? 为什么不能使用非成员函数来重载赋值运算符? - Why cannot a non-member function be used for overloading the assignment operator?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM