简体   繁体   English

你能解释这个成员函数模板吗?

[英]Can you explain this member function template?

Any one help me by explaining the real use of function templates. 任何人都可以通过解释函数模板的实际用途来帮助我。 How to they work? 他们如何工作? This morning I saw some code but i still don't understand the real use of this: 今天早上我看到了一些代码,但我仍然不明白这个的实际用途:

class A
{
template<class T> T getData() const
{
   const T* pointer == dynamic_cast<const T*>(mData)
   if(0 == pointer)
    {
     T defaultValue = T()
    }
  }
private:
  LData *mData; 
};

I don't understand anything of this. 我什么都不懂。 Can anyone give me a general idea about function templates? 谁能给我一个关于功能模板的一般概念?

Thanks 谢谢

I think the should be something like this: 我认为应该是这样的:

class A
{
template<class T> 
T getData() const
{
   const T* pointer = dynamic_cast<const T*>(mData);
   if(0 == pointer) 
     return T();

   return *T;
}
private:
  LData *mData; 
};

It's trying to get the mData converting that buffer or class (I don't know what LData is) in a T type. 它试图让mData转换为T类型的缓冲区或类(我不知道LData是什么)。

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

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