简体   繁体   English

纯虚函数重载

[英]Overload of pure virtual function

I usually use pure virtual functions for those methods that are required by my code to work well. 我通常使用纯虚函数来处理我的代码所需的那些方法。 Therefore, I create interfaces and then other users implement their derived classes. 因此,我创建接口,然后其他用户实现其派生类。 The derived classes have only these virtual functions as public while some additional methods should be implemented as private since my code does not call them. 派生类只将这些虚函数作为公共函数,而一些其他方法应该作为私有实现,因为我的代码不会调用它们。 I don't know if this can be considered as a good practice of OOP (are there any design pattern?). 我不知道这是否可以被认为是OOP的一个好习惯(有没有设计模式?)。 Anyway, my question is: Can a user overload a pure virtual function? 无论如何,我的问题是:用户可以重载纯虚函数吗?

ie

class Base
{
public:
 Base();
 virtual ~Base();
 virtual void foo(int,double)=0;
};

class Derived:
public Base
{
 private:
  // methods
 public:
 Derived();
 virtual ~Derived();
 virtual void foo(int, double, double); //this doesn't work
 };

A solution could be: 解决方案可能是:

 virtual void foo(int,double,double=0)=0;

in the base class but it is very limited. 在基类但它是非常有限的。 What do you think about? 你有什么想法?

These 2 functions are different. 这两个功能是不同的。 The latter is not overriding the first 后者并不压倒第一个

virtual void foo(int,double)=0;
virtual void foo(int, double, double);

The second one is new virtual function specific to derived. 第二个是特定于派生的新虚函数。

If you put a override at the end the compile will complain that you are not overriding anything. 如果你在最后放置一个override ,编译会抱怨你没有覆盖任何东西。 This is c++11 check though. 这是c ++ 11检查。

virtual void foo(int, double, double) override;

The user can override a pure virtual function to confirm use override at the end of function to verify. 用户可以覆盖纯虚函数以确认函数末尾的使用override以进行验证。 In your case the second function can only be accessed using Derived pointer or type. 在您的情况下,只能使用派生指针或类型访问第二个函数。 (although it cannot be instantiated unless the pure virtual function is properly overridden and implemented, untill then it is an abstract class). (虽然除非正确地重写和实现纯虚函数,否则无法实例化,直到它是一个抽象类)。 Hence if it is not to be intended to be overidden further by classes that derives from Derived then making it virtual is a overhead as anyway it is not overidding the base method. 因此,如果不打算通过Derived的类进一步覆盖它,那么使它成为虚拟是一种开销,因为它无论如何都不会覆盖基本方法。

You can't. 你不能。

Suppose you have a Base pointer, pointing to a Derived object. 假设您有一个Base指针,指向Derived对象。 Having the Base pointer, you have "access" only to the Base 's interface (unless you cast to Derived pointer, but this is not what you need). 使用Base指针,您只能“访问” Base的接口(除非您转换为Derived指针,但这不是您需要的)。

An overloaded function is merely a function with the same name as another, but accepting a different set of parameters, ie it is a different function. 重载函数只是一个与另一个函数具有相同名称的函数,但接受一组不同的参数,即它是一个不同的函数。 It has nothing to do with whether one or more overloaded functions is virtual or not. 它与一个或多个重载函数是否为虚拟无关。

In the example you presented, I believe a user could overload the pure-virtual function, but only after overriding it. 在您提供的示例中,我相信用户可以重载纯虚函数,但只能覆盖它。 And you couldn't access the function from the base class directly - you'd need to cast the base class pointer to the derived class pointer. 并且您无法直接从基类访问该函数 - 您需要将基类指针强制转换为派生类指针。

As pointed out by others, it just won't work. 正如其他人所指出的那样,它是行不通的。

More discretely, here is what happens 更离散的是,这是发生的事情

 Derived d;
 d.foo(5, 10.0); // Works as expected

 Base &b = d; // b is polymorphic
 b.foo(3,10,4); // foo(int, double, double) is not in class Base, hence compile-time resolution fails!

It is not overriding as the function signatures are different. 由于功能签名不同,因此不会覆盖。 According to polymorphism rule to override a function the function signatures and types should be same. 根据多态性规则覆盖函数,函数签名和类型应该相同。

In this case these functions are different. 在这种情况下,这些功能是不同的。 virtual void foo(int,double)=0; virtual void foo(int,double)= 0; virtual void foo(int, double, double); virtual void foo(int,double,double);

Isn't overloading foo in the base class the easiest solution? 是不是在基类中重载foo最简单的解决方案?

class Base
{
public:
 Base();
 virtual ~Base();
 virtual void foo(int,double)=0;
 virtual void foo(int,double,double)=0;
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM