C++ 如何使父对象实例化其子对象?
我有一个父类测试,子类是子测试。当我调用test->run()时,我想运行所有的子测试。以下代码不适用于b.c.父类中无法识别子类C++ 如何使父对象实例化其子对象?,c++,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)) {
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;
}
};
使用名称空间std;
课堂测试
{
受保护的:
简短冗长;
公众:
无效设置详细信息(短v)
{
如果((v==0 | | v==1 | | v==2))
{
冗长;
}
其他的
{
cout(0)
{
pow2*=2;
位计数++;
}
最大值=功率2-1;
故障值=功率2;
int native1=位计数;
int native2=sizeof(T)*8;
int native3=数字限制::数字;
如果(!signedType)
{
native1++;
native3++;
}
如果(verbosity_>=1)不能可能更好的方法是在test
类中创建一个名为run
的纯虚拟函数,然后子类的测试将实现它,并且当您在子类的实例上调用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....
}
};
当您通过测试*
(或测试&
)进行测试时,它们仍将运行:
您也可以在这里使用CRTP,但我认为这是不必要的。为什么不使用合成器模式
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();
}
类测试{
std::向量子测验;
公众:
无效添加子测试(测试t){
子测验。添加(t);
}
虚拟布尔点测试()=0;
无效运行(){
对于(int i=0;idoTest();
}
从父类而不是子类运行所有测试更有意义-classes@stack.user.1为什么本地人需要从测试中继承?但是,是的……我可能会这样做……因为从父类继承的简单方法现在似乎不太合适,只是因为冗长……从概念上讲,这是有意义的……测试and子测试…类别和子类-classes@stack.user.1如果natives是test的一个子类,那么natives必须知道如何运行自己的测试,而不是相反。另外,谁“运行”测试并不重要,用户将无法知道谁在做什么,因为语法完全相同。what do.add(t)做……什么是添加…是这个用户定义还是在一个库?@堆栈?用户1。这是C代码不完全转换为C++代码,它已经成为两者之间的混合,两者都无效。
Test* t = new Natives;
t->run(); // runs Natives::run
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();
}