编译时间检查方法是否定义为virtual [英] Compile time check of whether a method defined as virtual

查看:80
本文介绍了编译时间检查方法是否定义为virtual的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图想出一种方法检查派生类是否一个基类的方法被定义为'虚拟'。基本上我想有以下代码:

I'm trying to come up with a way of checking in the derived class whether a method of the base class is defines as 'virtual' . Basically I would like to have the following code:

class A {
  virtual void vfoo() {}
  void foo() {}
  virtual ~A() {}
};

class B : public A {
  virtual void vfoo() {
    MAGIC_CHECK(m_pA->vfoo()); // succeed
    // code
    m_pA->vfoo();
    // code
  }
  virtual void foo() {
    MAGIC_CHECK(m_pA->foo()); // fail compilation because foo is not virtual!
    // code
    m_pA->foo();
    // code
  }
  A * m_pA;
};



<
一个解决方案是使用-Woverloaded-virtual编译标志。
任何人都可以提出不涉及此标志的解决方案?

The question is, how do I implement this MAGIC_CHECK? One solution for this could be using -Woverloaded-virtual compilation flag. Can anyone suggest a solution that will not involve this flag?

谢谢!

推荐答案

在C ++ 03标准中,不可能检查方法是否声明为 virtual

您可以按照

In C++03 standard, it's not possible to check if a method is declared as virtual or not.
You may follow,


  • 编码标准

  • li>
  • 可能有一些静态分析工具

这篇关于编译时间检查方法是否定义为virtual的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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