如何让父实例化其子项?

时间:2011-11-18 21:33:29

标签: c++

我有一个父类测试,子类是子测试。当我调用test-> run()时,我想运行所有子测试。下面的代码不适用于b.c.父类中无法识别子类。

using namespace std;
  class test
    {
    protected:
      short verbosity_;
    public:
      void setVerbosity(short v)
        {
        if((v==0 || v==1 || v==2)) 
          {
          verbosity_ = v;
          }
        else 
          {
          cout << " Verbosity Level Invalid " << endl;
          }
        }
      void run()
        {
        natives natives_1; // this does not work
      }
    };
  class natives : public test 
    {
    public:
      natives()
        {
        this->run();
        }
      void run()
       {
       testInts<short>();
       testInts<int>();
       testInts<long>();
       testInts<unsigned short>();
       testInts<unsigned int>();
       testInts<unsigned long>();
       }         
    protected:
      template<class T> void testFloats()
        {
        }
      template<class T> void testInts()
        {
        short passState, bitDepth;
        T failMax, pow2 = 1, minValue = 0, maxValue = 0, bitCount = 0, failValue = 0;  
        const char* a = typeid(T).name();
        bool signedType = ((*a == 't') || (*a == 'j') || (*a == 'm'));
        while(pow2 > 0)
          {
          pow2 *= 2;
          bitCount++;
          }
        maxValue = pow2-1;
        failValue = pow2;
        int native1 = bitCount;
        int native2 = sizeof(T)*8;
        int native3 = numeric_limits<T>::digits;  
        if( !signedType )
          {
          native1++;
          native3++;
          }       
        if(verbosity_>=1) cout << endl << "**********\n" << reportType(a) << "\n**********" << endl << endl;
        if ((native1 == native2) && (native1 == native3))
          {
          if(verbosity_>=1)cout << "Correlation:\t\tPass: " << native1 << endl ;
          if(verbosity_>=2)
          {
            cout << "--Algorithm:\t\t" << native1 << endl;
            cout << "--Sizeof:\t\t" << native2 << endl;
            cout << "--Reported:\t\t" << native3 << endl;
            cout << "----Max Value:\t\t" << maxValue << endl;
            cout << "----Max+1\t\t" << failValue << endl;
            } 
         else
            {
            }
          }
        else
          {
          cout << "Correlation:\t\tFail" << 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;
        }
};

2 个答案:

答案 0 :(得分:1)

可能更好的方法是在run类中创建一个名为test的纯虚函数,然后子类的测试将实现它,那些将在你运行时运行在子类的实例上调用run

例如:

class Test {
public:
    virtual void run() = 0;

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

class Natives : public Test {
public:
    void run() {
        // do all the tests....
    }
};

然后你可以做

Natives n;
n.run();

当您通过Test*(或Test&)执行此操作时,它们仍然可以运行:

Test* t = new Natives;
t->run(); // runs Natives::run

您也可以在这里使用CRTP,但我认为这是不必要的。

答案 1 :(得分:0)

为什么不使用合成器模式

class Test {

std::vector<Test> subTests;

public:
void addSubTest(Test t) {
    subTests.add(t);
}

virtual bool doTest() = 0;

void run() {
   for(int i=0; i< subTests.size; ++i) {
       subTests[i].doTest();
   }
   this->doTest();
}