1

I have a templated base class that provides a method remove(). I have a class derived from the templated base class that does not hide the remove() method. However, the templated based classes' remove method isn't visible. Why? And is there a way to resolve this (I mean other than the 'trick' I figured out at the very end)?

I've stripped this down to a small code example:


#include <map>
#include <iostream>
#include <boost/shared_ptr.hpp>



// Common cache base class. All our caches use a map, expect children to
// specify their own add, remove and modify methods, but the base supplies a
// single commont remove too.
template <class T>
class cache_base {
public:

    cache_base () {};

    virtual ~cache_base() {};

    virtual void add(uint32_t    id) = 0;

    virtual void remove(uint32_t    id) = 0;

    void remove() {
        std::cout << "This is base remove\n";
    };

    virtual void modify(uint32_t    id) = 0;

protected:
    typedef std::map< uint32_t, typename T::SHARED_PTR_T>    DB_MAP_T;

    DB_MAP_T    m_map;
};


// A dummy item to be managed by the cache.
class dummy {
public:
    typedef    boost::shared_ptr<dummy>    SHARED_PTR_T;

    dummy () {};
    ~dummy () {};
};


// A dummy cache
class dummy_cache :
    public cache_base<dummy>
{
public:
    dummy_cache () {};

    virtual ~dummy_cache () {};

    virtual void add(uint32_t    id) {};

    virtual void remove(uint32_t    id) {};

    virtual void modify(uint32_t    id) {};
};




int
main ()
{
    dummy_cache    D;

    D.remove();

    return(0);
}

This code fails to compile, giving me the following errors


g++ -g -c -MD -Wall -Werror -I /views/LU-7.0-DRHA-DYNAMIC/server/CommonLib/lib/../include/ typedef.cxx
typedef.cxx: In function 'int main()':
typedef.cxx:67: error: no matching function for call to 'dummy_cache::remove()'
typedef.cxx:54: note: candidates are: virtual void dummy_cache::remove(uint32_t)
make: *** [typedef.o] Error 1

I don't know if it makes a difference but I am using g++ version 4.1.2 20070115.

Also, I found that if I added the following remove method to the dummy_cache it works. However, it feels odd that I have to add an a subordinate method in the dummy_cache to expose a public base method.

void remove () {return cache_base<dummy>::remove(); }
John Rocha
  • 1,656
  • 3
  • 19
  • 30
  • possible duplicate of [C++ override/overload problem](http://stackoverflow.com/questions/1484641/c-override-overload-problem) – Bo Persson Apr 18 '12 at 19:37

1 Answers1

5

Your overloaded dummy_cache::remove(uint32_t) is hiding the cache_base::remove(). You can unhide with:

class dummy_cache :
    public cache_base<dummy>
{
public:
  using cache_base<dummy>::remove;
  ...
};
chrisaycock
  • 36,470
  • 14
  • 88
  • 125
  • 1
    Wow! I didn't think the overloaded method `remove(uint32_t)` would hide the base classes `remove()` method. I also tested it out with a non-templated base class and sure enough this is how it is! _Thank you_ – John Rocha Apr 18 '12 at 19:31
  • @JohnRocha The only reason I knew the answer is that I had run into the [exact same problem](http://stackoverflow.com/q/4597032/478288) last year. – chrisaycock Apr 18 '12 at 20:11