c++ - What is the correct way to call the pure virtual member when another member exists with another signature? -
i have pure virtual interface class , derived class this
class iinterface { public: virtual void func() = 0; }; class myclass : public iinterface { public: void func(int i) { func(); } };
the compiler complains call func()
in func(int i)
not take 0 arguments. correct way specify calling pure virtual member?
the 2 solutions came were
void func(int i) { static_cast<iinterface*>(this)->func(); }
and
void func(int i) { iinterface::func(); }
are either of these viable? there better way? both seem clunky.
bring base class declaration derived class scope using-declaration:
class myclass : public iinterface { public: using iinterface::func; void func(int i) { func(); } };
alternatively, convert this
pointer base work (i.e., first option):
void func(int i) { static_cast<iinterface*>(this)->func(); }
iinterface::func();
won't work, explicit qualification disables virtual dispatch.
Comments
Post a Comment