简体   繁体   English

C ++中模板参数的类继承

[英]Class inheritance of template parameters in C++

I am new to C++ and happen to come across a code which looks like this : 我是C ++的新手,碰巧遇到了一个看起来像这样的代码:

template<class T, class Composite> class CompositeTester: public Composite
{
  public: 
    CompositeTester(T* instance, const typename Composite::Parameters& parameters) : Composite(parameters)
    {
        singletonInstances_[parameters.instanceIndex] = instance;
    }
}
  1. The inheritance is not so clear to me, because the inheritance is from the template class arguments itself. 继承对我来说还不太清楚,因为继承来自模板类参数本身。 What is this concept known as? 这个概念被称为什么?
  2. In the contructor CompositeTester , I realise that the instance of Composite is created with parameters as arguments. 在构造器CompositeTester ,我才知道的实例Composite与创建parameters作为参数。 But this syntax is quite difficult to understand const typename Composite::Parameters . 但是这种语法很难理解const typename Composite::Parameters How to intrepret this syntax? 如何解释这种语法? Is defining an object of class composite, even before it exists valid? 定义类复合对象是否有效?

  3. singletonInstances_[parameters.instanceIndex] = instance . singletonInstances_[parameters.instanceIndex] = instance Is here a new variable created for parameters.instanceIndex ? 这里是为parameters.instanceIndex创建的新变量吗? There exists no definition in the source code for class Composite::Parameters or class Composite apart from what I mentioned in the question here. 除了我在这里的问题中提到的内容之外, class Composite::Parametersclass Composite的源代码中没有定义。

  1. This is known as the curiously recurring template pattern . 这就是奇怪的重复模板模式

  2. typename is used here to denote a dependent type name. typename在此处用于表示从属类型名称。 Without it, the compiler will parse the qualified name as a non-type entity. 没有它,编译器会将合格名称解析为非类型实体。 See our FAQ on this: Where and why do I have to put the "template" and "typename" keywords? 请参阅以下常见问题解答: 为什么必须在何处以及为什么要放置“模板”和“类型名”关键字?

  3. This is ill-formed in standard C++ because singletonInstances_ is not declared. 这在标准C ++中格式错误,因为未声明singletonInstances_ If it is declared in the base class, you need to use this-> to make it a dependent name. 如果在基类中声明了它,则需要使用this->使其成为从属名称。

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

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