从基函数继承,导出函数的存在性 [英] Inheritance, existance of derived function from base function

查看:77
本文介绍了从基函数继承,导出函数的存在性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有遗传问题。假设我有两个类,一个是基类,另一个是派生类。这两个类都是从CWnd派生的MFC类,所以我可以使用CRuntimeClass来知道我正在使用哪个类。我需要知道的是,如果有一种方法,我可以从基类中判断出我所在的函数是否已在派生类中被覆盖。



在我的情况下,我正在看CWnd :: OnContextMenu()。文档说如果你不在你的覆盖中处理消息,你必须调用Default(),这是CWnd :: OnContextMenu()所做的全部。如何调用Default()是否条件OnContextMenu()没有被覆盖?



使用CRuntimeClass我可以检查基类是否派生自,但是如何检查OnContextMenu()是否已被覆盖?



I have an inheritance question. Say I have two classes, one a base class and another a derived class. Both classes are MFC classes derived from CWnd, so I can use CRuntimeClass to know which class I am working with. What I need to know is if there is a way I can tell, from the base class, if the function I am in has been overridden in the derived class.

In my case I am looking at CWnd::OnContextMenu(). The documentation says that if you do not handle the message in your override you must call Default(), which is all that CWnd::OnContextMenu() does. How can I make the call to Default() be conditional on OnContextMenu() not being overridden?

Using CRuntimeClass I can check if the base class has been derived from, but how can I check if OnContextMenu() has been overridden?

class CBaseWnd : public CWnd
{
...
    afx_msg void OnContextMenu(CWnd* pWnd, CPoint point)
    {
        bool value = CallSomeFunction(pWnd, point); // may popup menu?

        if (false == value && !IsDerivedFunction())
        {
            Default();
        }

        SetReturnValue(value);
    }

    bool IsDerivedFunction()
    {
        CRuntimeClass* pRTC = GetRuntimeClass();
        // pRTC->m_lpszClassName will be "CDerivedWnd"
        //
        // How can I check for existance of CDerivedWnd::OnContextMenu
...
};


class CDerivedWnd : public CBaseWnd
{
...
    afx_msg void OnContextMenu(CWnd* pWnd, CPoint point)
    {
        CBaseWnd::OnContextMenu(pWnd, point);
        bool value = GetReturnValue();

        if (false == value)
        {
            value = CallSomeOtherFunction(pWnd, point); // may popup menu?
        }

        if (false == value)
        {
            Default();
        }
    }
};





我尝试使用#stringizing运算符使用宏## tokenizing运算符没有运气。这甚至是可能还是我咆哮错误的树可以这么说?



I tried using macros by using the # stringizing operator and the ## tokenizing operator with no luck. Is this even possible or am I barking up the wrong tree so to speak?

推荐答案

这将完全滥用面向对象的功能。 OOP背后的主要思想之一是使用类类型不可知的实例。为此,已创建运行时类型编译时类型之间的差异。编译器使用某种类型的变量,这是一种编译时类型,但是这个变量可以分配给某些派生类型的对象,它将是运行时类型。使用此实例的代码仍然不知道实际类型。它是在基于编译时类型的假设中编译的,哪个服务器作为两个或多个派生类型的公共接口。此外,可以重写直接或间接调用此变量的某些函数以实现不同的行为。它们是动态调度的,因此在基类型中定义的函数将调用一些虚方法,但调度到此内部调用的实际实现将取决于它的运行时类型。



现在,面向对象设计的主要原则之一是保持不可知代码不可知。了解运行时类型绝不是目标。真正的目标是获得一些正确的行为。你需要设计你的类层次结构,最重要的是,虚拟函数被覆盖或不被覆盖,实现该功能的方式。



所以,没有积极的建议你 - 你走错了方向。我只能告诉你什么可以帮助你。首先,学习OOP的想法和技巧。我希望你的错误可以成为一个好的思想来源和一个好的教训。但首先,你需要意识到你的问题究竟是什么。我可以告诉你它是什么:你创建你的类层次结构,虚拟函数和覆盖的唯一目的是:创建一些东西来对抗,而不是帮助实现你的目标。



如果您能够制定真正的目标,我们可以考虑具体的解决方案。我担心即使这对你来说也很困难。拜托,你必须意识到你的目标的概念是什么。你不能说,例如,我想知道类型,或我想要覆盖,或我想创建一个......。这些可能是解决问题的一些可能工具,但不是目标。换句话说,你应该从你现在的想法中清楚地解决你的问题,如何解决它,提出一个明确的问题。小心试试?



-SA
This would be a total abuse of object-oriented functionality. One of the major ideas behind OOP is making the use of instances of classes type-agnostic. For this purpose, the difference between the runtime types and compile-time types has been created. The compiler works with a variable of some type, which is a compile-time type, but this variable can be assigned to the object of some derived type, which will be the runtime type. The code using this instance remains unaware of actual type. It is compiled in the assumptions based on on the compile-time type, which server as a common interface for two or more derived types. Moreover, some of the functions called on this variable, directly or indirectly, can be overridden to achieve different behavior. They are dispatched dynamically, so a function defined in a base type will call some virtual method, but actual implementation dispatched to this inner call will depend on what runtime type is that.

Now, one of the main principle of object-oriented design is too keep agnostic code agnostic. Knowing the runtime type is never a goal. The real goal is to get some correct behavior. You need to design your class hierarchy, and, most importantly, the virtual functions, overridden or not, the way to achieve that functionality.

So, no positive advice for your — you went too far in wrong direction. I only can tell you what can help you. First of all, learning of OOP ideas and techniques. I hope your mistake can become be a good source of ideas and a good lesson to learn. But first of all, you need to realize what your problem really is. I can tell you what is it: you create your class hierarchy, virtual functions and overrides with the only purpose: to create something to struggle against, instead of helping to achieve your goals.

If you can formulate your real goal, we can consider concrete solutions. I'm afraid even that could be difficult for you. Please, you have to realize what is the concept of the "goal" in your case. You cannot say, for example, "I want to know the type", or "I want to override", or "I want to create a class which…". Those would be some possible tools for solving problems, but not the goals. In other words, you should clean up formulation of your problem from your present-day thinking on how it can be solved, present a clear problem. Care to try?

—SA


这篇关于从基函数继承,导出函数的存在性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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