Reputation: 11
someone know by chance about a workaround to call non-static method from static method in abstract class in C++?
for example, i have the following abstract class:
class aClass
{
public:
static int check();
virtual int check_deep()=0;
}
and i want to call to check_deep() form check()
thanks in advance, Judith.
Upvotes: 1
Views: 177
Reputation: 1
You'll need to change your static method's signature to receive a reference (or pointer) to an instance of aClass
to do so:
class aClass
{
public:
static int check(aClass& instance);
virtual int check_deep()=0;
}
int aClass::check(aClass& instance)
{
return aClass.check_deep();
}
The design smells somehow though!
Upvotes: 2
Reputation: 20993
No way to do it. Non-static method requires an object to act on, and in your case when it is pure virtual the object even decides which exact method will be called. You absolutely need an object of aClass type to call check_deep.
Upvotes: 0
Reputation: 33046
No. And it would not make sense, since a non-static method is bound to an instance of the class (to which you refer as this
) while a static method is not bound to any instance: there would be no this
to bind check_deep
to from check
.
Obviously, supposing that you will be calling it from a subclass, otherwise check_deep
is pure virtual and you won't be able to invoke it.
Upvotes: 0