C ++强制方法覆盖在具体类中 [英] C++ Forcing Method Override In Concrete Class

查看:87
本文介绍了C ++强制方法覆盖在具体类中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一种方法在C ++写一个具体的类,当另一个类派生自它,有一个方法必须被覆盖。抽象类允许强制派生类创建任何抽象方法的具体版本,但是我想要的是一个强制执行的基类,但也可以使用它自己的权限。我知道抽象方法也可以指定默认的功能,但这仍然是一个抽象类,不能实例化。



我也看了模板方法模式,但这不'

解决方案

我会假设你正在寻找一个编译时强制执行这个条件(感谢@Chad指出它)



我知道C ++中没有直接的语言机制。我的意思是,没有一个保留的关键字放在你的方法声明的前面,以实现你想要的目标。



我认为你说的是​​指向一个设计问题在您的软件。让我们假设你想强制foo()方法被以下代码片段中的所有继承类重新实现

  class BaseButConcrete 
{
... //常见的东西
... //

virtual void foo()
{/ *实现,你不想被继承,但将是... * /}
}

class DerivedOtherConcrete:public BaseButConcrete
{
void foo()
{/ *不同的实现,
,但从编译器的角度没有义务* /}
}

我可以看到没有好的设计原因,为什么所有的常见的东西不能移动在抽象基类。根据您的描述,您不希望继承Derived中的 foo 实施,因此不要继承该部分!因此,非常经典的设计应该做的诀窍:

  class AbstractBase 
{
... //常见的东西已经移动到这里
... //
virtual void foo()= 0;
}

class NotAnymoreBaseButStillConcrete:public AbstractBase
{
void foo()
{/ *不应继承的实现,
不会通过设计* /}
}

class DerivedOtherConcrete:public AbstractBase
{
void foo()
{/ *不同的实现,
现在强制编译器为类具体* /}
}

这样,常见的东西仍然在所有派生类之间共享,并且保留了你不想继承的东西(即 foo 实现)在不在同一继承路径中的类中分隔。 / p>

Is there a way in C++ to write a concrete class which when another class is derived from it, has a method which must be overriden. An abstract class allows the forcing of the derived class to create concrete versions of any abstract methods but what I want is a base class that enforces this but can also be used in its own right. I know an abstract method can also specify a default functionality but this still remains an abstract class which cannot be instantiated.

I also looked at the template method pattern but this doesn't seem to be quite what I am looking for either.

解决方案

I will assume you are looking for a compile-time enforcing of this condition (thank you @Chad for pointing it out)

There is no direct language-mechanism in C++ that I know of. I mean, there is not a reserved keyword to put in front of your method declaration that would achieve your desired goal.

I think that what you say is pointing to a design problem in your software. Let's say you want to force the foo() method to be reimplemented by all inherirhing classes in the following snippet

class BaseButConcrete
{
    ... //Common stuff
    ... //

    virtual void foo()
    { /*implementation that you do not want to be inherited, but will be...*/ }
}

class DerivedOtherConcrete : public BaseButConcrete
{
    void foo()
    { /*different implementation, 
      but no obligation from compiler point of view*/ }
}

I can see no good design reason why all the common stuff could not be moved in an abstract base class. From what you describe, you do not want to inherit the foo implementation in Derived, so do not inherit that part ! Thus the very classic design should do the trick :

class AbstractBase
{
    ... //Common stuff has moved here
    ... //
    virtual void foo() =0;
}

class NotAnymoreBaseButStillConcrete : public AbstractBase
{
    void foo()
    { /*implementation that should not be inherited,
      and that will not by design*/ }
}

class DerivedOtherConcrete : public AbstractBase
{
    void foo()
    { /*different implementation, 
      now mandatory compiler-wise for the class to be concrete*/ }
}

This way, the common stuff is still shared among all your derived classes, and you keep what you do not want to inherit (i.e. the foo implementation) separated in classes not on the same inheritance path.

这篇关于C ++强制方法覆盖在具体类中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆