2

Does anyone knows why this code cannot be compiled in visual studio 2013? The problem lies in this that b.a() has only one version (the overrided one in B class a(float)) and version a(std::string) is unavailable although it is in base class.

#include <string>

template <typename T>
class A { 
public:
    virtual void a(std::string b){ this->a(123); }
    virtual void a(float b) = 0;
};

class B : public A < std::string > {
public:
    virtual void a(float b) override {}
};


main()
{
    B b;

    b.a(""); // Error here: error C2664: 
             // 'void B::a(float)' : cannot convert argument 1 from 'const char [1]' to 'float'

    B* bb = new B();
    bb->a(""); // same
}
Michal W
  • 793
  • 8
  • 24

1 Answers1

5

If a derived class declares a name and you also want members of this name from base classes to be visible, you need to unhide those names explicitly, using using:

class B : public A<std::string>
{
public:
    using A<std::string>::a;
    virtual void a(float b) override {}
};

Now you can use all overloads of a:

B x;
x.a(1.2);
x.a("hello");
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • Da*n, u'r good! I'm a bit confused coz i met this problem first time although i write in C++ from some time... So if derived class override a function for ex. fun1 then all functions called fun1 from base class are automatically hidden in derived class (except overrided one)? – Michal W Oct 14 '14 at 19:10