简体   繁体   English

模板成员函数继承

[英]template member function inheritance

I have a non-template class with a template member function similar to below. 我有一个非模板类,其模板成员函数类似于下面。

class Component
{
public:
static const int HASH_ID = 0;
virtual int HashID() const {return Component::HASH_ID;}
virtual bool is(int hash_id) const {return Component::HASH_ID == hash_id;}
template<typename T> bool is() const {return this->is(T::HASH_ID);}
};

class Transform : public Component
{
 public:
 static const int HASH_ID = 1;
 virtual int HashID() const {return Transform::HASH_ID;}
 virtual bool is(int hash_id) const {return Transform::HASH_ID == hash_id;}
};

My issue is that the Transform class won't let me call the template function. 我的问题是Transform类不会让我调用模板函数。

Here is the test code I wrote that won't compile 这是我编写的无法编译的测试代码

int main(void)
{
  Transform test_component;
  Transform* transform = &test_component;
  Component* component = &test_component;

  std::cout << component->is<Transform>() << std::endl; //No issues

  //Won't compile unless I comment out this line
  std::cout << transform->is<Transform>() << std::endl; //Says I am missing argument list

  std::cout << transform->is(Transform::HASH_ID) << std::endl; //Works Fine!
  return 0;
}

Any ideas why this doesn't work? 任何想法为什么这不起作用?

virtual bool is(int hash_id) const {return Transform::HASH_ID == hash_id;}

is hiding 藏着

template<typename T> bool is() const {return this->is(T::HASH_ID);}

If a derived class declares a method with the same name as a method defined by the base class, the derived class' method hides the base class' one. 如果派生类声明了一个与基类定义的方法同名的方法,则派生类'方法会隐藏基类'one。

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

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