c++ - 当它们在父类(super class)中被定义为纯虚拟时,如何从子类中调用所有函数?

标签 c++ class hierarchy pure-virtual

主要问题是我如何实现 startTest() 以便它在所有子类中调用 runTest。谢谢!

/*******************
COMPILER TEST
*******************/

class archeTest
  {
  protected:
    short verbosity_;

  public:

    void setVerbosity(short v)
      {
      if( ((v == 1) || (v == 0) ) 
        {  
        verbosity_ = v;
        }
      else 
        {
        cout << " Verbosity Level Invalid " << endl;
        }
      }

    virtual void runTest() = 0;
      {
      }

    void startTest()
      {
      }
  };

class testNatives : public archeTest 
  {
  public:

    void runTest()
      {
      testInts<short>();
      testInts<int>();
      testInts<long>();
      testInts<unsigned short>();
      testInts<unsigned int>();
      testInts<unsigned long>();
      }      

    void reportResults() const
      {
      }

  protected:

    template<class T> void testFloats()

    template<class T> void testInts()
      {

      verbosity_ = 1;  

      T     failMax;
      short passState;
      short bitDepth;

      const char* a = typeid(T).name();
      bool signedType = ((*a == 't') || (*a == 'j') || (*a == 'm'));

      /* Bit Depth - Algorithm */

      T pow2 = 1, minValue = 0, maxValue = 0, bitCount = 0, failValue = 0;  
      while(pow2 > 0)
        {
        pow2 *= 2;
        maxValue = pow2-1;
        bitCount++;
        }
      failValue = pow2;

      int native1 = bitCount;
      int native2 = sizeof(T)*8;
      int native3 = numeric_limits<T>::digits;  
      if( !signedType )
        {
        native1++;
        native3++;
        }       
      if(verbosity_)
        {
        cout << endl << "**********\n" << reportType(a) << "\n**********" << endl << endl;
        cout << "Bit Depth - Algorithm:\t" << native1 << endl;
        cout << "Bit Depth - Sizeof:\t" << native2 << endl;
        cout << "Bit Depth - File:\t" << native3 << endl;
        }   
        if (native1 == native2 && native1 == native3)
          {
          cout << "Correlation:\t\tPass" << endl ;
          }
        else
          {
          cout << "Correlation:\t\tFail" << endl ;
          }
        cout << "Max Value:\t\t" << maxValue << endl;
        cout << "Max+1 Value:\t\t" << failValue << endl;
      } 

    string reportType(const char* c1)
      { 
      string s1;
      switch(*c1)
        {
        case 't':
          s1 = "Unsigned short";
          break;
        case 'j':
          s1 = "Unsigned int";
          break;
        case 'm':
          s1 = "Unsigned long";
          break;
        case 's':
          s1 = "Short";
          break;
        case 'i':
          s1 = "Int";
          break;
        case 'l':
          s1 = "Long";
          break;
        default:
          s1 = "Switch failed";
        }
      return s1;
      }
  };

int main()
  {
  testNatives A;
  A.runTest();
  } 

最佳答案

这是可能的,但要做到这一点,您将不得不使用抽象工厂模式。请read this了解什么是抽象模式以及它如何满足您的需求。

如果您可以在您的项目中使用 boost,那么您可以使用 boost::factory template 实现您自己的抽象工厂。 .

如果您不想推出自己的抽象工厂,您可以使用许多其他的抽象工厂实现。 Here是一个这样的实现的链接。

编辑:在这种情况下,您还需要一些机制在编译时向工厂注册新的测试用例。这可以通过利用 c++ 预处理器或模板来实现。 Here是一种使用模板的方法。

关于c++ - 当它们在父类(super class)中被定义为纯虚拟时,如何从子类中调用所有函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6038430/

相关文章:

c++ - boost::asio 和 send() 的使用

java - 第一个Java程序(计算器)问题

c++ - 尝试支持文字时构造函数重载歧义

CSS 层次结构类和 ID

php - 如何合并/消除类文件的 PHP 包含

c++ - 严格的别名似乎不一致

c++ - 是否可以在不为该数组创建变量的情况下将数组作为参数传递给函数?

c++ - 有效地为地形物理提供几何

c++ - 是否可以在 C++ 中授予顶级函数访问对象成员的权限?

php - 为什么不调用父构造函数?