What is the elegant way to `Using` polymorphic functions from base classes in c++?

Issue

This Content is from Stack Overflow. Question asked by Voile

This code compiles.

#include <bits/stdc++.h>
using namespace std;

class PA {};

class PB {};

class A {
public:
  void foo(PA) { cout << "A PA" << endl; }
  void foo(PB) { cout << "A PB" << endl; }
};

class B : public A {
public:
  // void foo(PA) { cout << "B PA" << endl; }
};

int main() {
  B b;
  PB PB;
  b.foo(PB);
  return 0;
}

Uncomment line 16 and the compilation will fail.

foo.cpp: In function ‘int main()’:
foo.cpp:22:9: error: cannot convert ‘PB’ to ‘PA’
   22 |   b.foo(PB);
      |         ^~
      |         |
      |         PB
foo.cpp:16:12: note:   initializing argument 1 of ‘void B::foo(PA)’
   16 |   void foo(PA) { cout << "B PA" << endl; }
      | 

This problem can be solved by using A::foo;. But I ran into a situation where there are multiple polymorphic functions in the base class. using for each of them seems silly.

Is there a way to import all functions of the base class like using A::*;? Or should this not happen at all and I should re-architecture the code?

Thanks!



Solution

This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?