1

我有一个提供方法 remove() 的模板化基类。我有一个从模板化基类派生的类,它不隐藏 remove() 方法。但是,基于模板的类的 remove 方法是不可见的。为什么?有没有办法解决这个问题(我的意思是除了我最后想出的“技巧”)?

我已将其简化为一个小代码示例:


#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);
}

此代码无法编译,给我以下错误


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

我不知道它是否有所作为,但我使用的是 g++ 版本 4.1.2 20070115。

另外,我发现如果我将以下删除方法添加到dummy_cache它的工作原理。但是,我不得不在 dummy_cache 中添加一个从属方法来公开一个公共基础方法,这感觉很奇怪。

void remove () {return cache_base<dummy>::remove(); }
4

1 回答 1

5

您的重载dummy_cache::remove(uint32_t)正在隐藏cache_base::remove(). 您可以通过以下方式取消隐藏:

class dummy_cache :
    public cache_base<dummy>
{
public:
  using cache_base<dummy>::remove;
  ...
};
于 2012-04-18T18:56:40.200 回答