Reputation: 165
I have a BaseInterface class, with a virtual void execute() to be implemented by derived classes:
class BaseInterface
{
public:
virtual void execute(int a) = 0;
}
And I have tons of derived classes overwriting the execute void:
class ProcessN : public BaseInterface
{
public:
void execute(int a);
}
Execute void of one of my derived classes has a bug. But there are tons of derived classes. It is very difficult to check each of them one by one. I have difficulties in finding the bug.
Is there a method in C++ to find out via base class, which derived class is currently processing over it?
EDIT: Ok I improve my question after a useful discussion on the comments:
May I implement something inside the constructor of the BaseInterface class, to print out the info of the current processing derived class?
Upvotes: 2
Views: 141
Reputation: 63
How is execute being invoked....May be you can introduce a static method in base class which takes a Baseclass pointer as argument and call this method before execute and print the necessary information from the derived object in this method using BasePointer to see which derived class object it is. Per my understanding I suggested as below. If it does not help All the best.
class Base
{
public:
virtual void exec(int i) =0 ;
static void check(Base* temp)
{
cout<<"Type of obj"<<typeid(*temp).name()<<endl;
}
};
class Derived1 : public Base
{
public:
void exec(int i)
{
cout<<"Der1 Exe "<<i<<endl;
}
};
class Derived2 : public Base
{
public:
void exec(int i)
{
cout<<"Der2 Exe "<<i<<endl;
}
};
int main()
{
Base *client = NULL;
Derived1 lder1;
Derived2 lder2;
client= &lder2;
Base::check(client);
client->exec(0);
client= &lder1;
Base::check(client);
client->exec(0);
return 0;
}
Upvotes: 0
Reputation: 3984
You are looking for typeid
BaseInterface *a = new Process1();
BaseInterface *b = new Process2();
cout << typeid(*a).name() << endl;
cout << typeid(*b).name() << endl;
Or if you want to use inside your execute() you can just simply use typeid(*this)
class BaseInterface
{
public:
virtual void execute(int a) = 0;
//debug helper
void print_info() { cout << typeid(*this).name() << endl; }
};
class ProcessN : public BaseInterface
{
void execute(int a) { print_info(); }
};
Upvotes: 1