メソッド 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 を使用しています。
また、次の remove メソッドを に追加すると、機能することがわかりましたdummy_cache
。ただし、パブリック ベース メソッドを公開するために、dummy_cache に下位メソッドを追加する必要があるのは奇妙に感じます。
void remove () {return cache_base<dummy>::remove(); }