简体   繁体   English

在具有不同模板参数集的模板化 class 中使用模板化 object

[英]Use templated object within a templated class with different sets of template parameters

I'm trying to use a templated object in a templated class where the template parameters of the object are not the same ones needed by the class.我正在尝试在模板化 class 中使用模板化 object,其中 object 的模板参数与 object 所需的模板参数不同。 It sounds a bit confusing because is not described correctly, here is an example.这听起来有点混乱,因为没有正确描述,这里有一个例子。

The following class implements a binary search tree:下面的 class 实现了一个二叉搜索树:

template <typename KEY, typename VALUE>
class BST{
  public:
    BST(void){
      cache = new GCACHE<uint64_t, BSTObject>(this);
    }

    struct{
     string value;
    }BSTObject;

    GCACHE<uint64_t, BSTObject>* cache;

    ...
}

the GCACHE class implements a generic cache, and to function correctly it needs a pointer to the BST it has to cache. GCACHE class 实现了一个通用缓存,并且对于 function 正确它需要一个指向它必须缓存的 BST 的指针。 This pointer is passed to the GCACHE constructor:这个指针被传递给 GCACHE 构造函数:

template <typename CKEY, typename OBJECT>
GCACHE{
  public:
   GCACHE( *** pointer to BST, a templated class whoose template types that are not CKEY and OBJECT ***); (A)
}

How can I specify/define in (A) a set of template parameters that is different to the one needed by GCACHE (CKEY and OBJECT in the example)?如何在 (A) 中指定/定义一组与 GCACHE 所需的模板参数不同的模板参数(示例中为 CKEY 和 OBJECT)? The template parameters of the GCACHE object are uint64_t and BSTObject and the template parameters of the BST object might be int and string . GCACHE object 的模板参数是uint64_tBSTObject ,而 BST object 的模板参数可能是intstring

I'm really confused.我真的很困惑。 Maybe I'm getting it all wrong and the solution is very simple... thank you for your help.也许我弄错了,解决方案很简单......谢谢你的帮助。

I hope that the above example is clear enough to obtain help.我希望上面的例子足够清楚以获得帮助。 If not please let me know so that I can rephrase the question.如果不是,请告诉我,以便我可以改写这个问题。

What parts of BST does GCACHE need access to? GCACHE需要访问BST的哪些部分?

If it only needs to access parts that don't depend on the template parameters KEY and VALUE , then create an abstract interface class that BST implements, and use that class as the pointer type for the GCACHE constructor:如果它只需要访问不依赖于模板参数KEYVALUE的部分,则创建BST实现的抽象接口 class ,并将该 class 用作GCACHE构造函数的指针类型:

class BSTInterface {
    ...
};

template<typename KEY, typename VALUE>
class BST : public BSTInterface {
    ...
};

template <typename CKEY, typename OBJECT>
GCACHE{
  public:
   GCACHE(BSTInterface *bst); (A)
};

If GCACHE does need to access parts of BST that depend on template parameters, then you'll need to make GCACHE templated on those parameters as well:如果GCACHE确实需要访问依赖于模板参数的BST部分,那么您还需要将GCACHE模板化为这些参数:

template <typename CKEY, typename OBJECT, typename KEY, typename VALUE>
GCACHE{
  public:
   GCACHE(BST<KEY, VALUE> *bst); (A)
};

By the way, in general such a circular dependency between classes creates tight coupling and might indicate a design issue.顺便说一句,一般来说,类之间的这种循环依赖会产生紧密耦合,并且可能表明存在设计问题。 Why would a "generic cache" need to know about the existence of a binary search tree?为什么“通用缓存”需要知道二叉搜索树的存在? Or, if the cache is implemented as a tree, why would the tree implementation need to know about the existence of the cache?或者,如果缓存被实现为树,为什么树实现需要知道缓存的存在?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM