c++ - Wrapper implementation for an abstract method in the base class -


i have base class inherited many subclasses. need define new signature abstract method, wrapper. tried

class b {  public:   virtual void f() = 0;   void f(string msg) { /* print msg , call f() */ } };  class d : public b {  public:   void f() override { /* implementatation */} };  int main() {   d d;   d.f("msg"); } 

but doesn't compile , give following error

error: no matching function call 'd::f(string) 

my questions are:

  1. why cannot d::f(string) resolved?
  2. why of following fix problem?
    • renaming f(string) f2(string) (ugly)
    • defining d::f(string x) { b::f(x)} (ugly has defined in every subclasses)
    • removing abstract method, b::f() (not acceptable)

any better solution?

the issue hid b::f(std::string) here:

class d : public b { public:   void f() override; }; 

when call d.f("msg"), name lookup find 1 f() in d , stop looking. first find candidate functions then perform overload resolution. since 1 happens take no arguments, compile error.

if want use other overload of f, need bring d well, so:

class d : public b { public:   using b::f;   void f() override; }; 

now have d::f() (the overridden virtual function) , d::f(std::string ) (which brought in b).

an alternative, trivial solution rename 1 or other function don't have problem begin with.


Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -