注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

hhfighting的博客

以责人之心责己,以恕己之心恕人

 
 
 

日志

 
 
 
 

template class usage  

2014-08-15 05:08:55|  分类: C & C++ |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
Today I met one error:
there are no arguments to ‘’ that depend on a template parameter, so a declaration of ‘’ must be available
This happen in the stituation which in derived class I call a function which defined in the base class. The base class is template class. A template class inherits from a template base class.
The solution for this question is I should use this->(function) instead of directly using (function).  You should explicitly invoke base class member functions in a class template through the this pointer.
The reason is:

If a template class inherits from a template base class, the way that name lookup works is slightly different from name lookup in a normal class. You can't just call base class's members using their names, you need to indicate to the compiler where to look for the name. Use this->*, the compiler knows that this function is a member of the current class, if can't find in the current class, they assume this function could be in the base class, so it will defer name lookup until the template is instantiated.

A little more about this knowledge:

the C++ standard defines a "two-phase name lookup" rule for names in templates. Names inside templates are divided to two types:

  • Dependent – names that depend on the template parameters but aren’t declared within the template.
  • Non-dependent – names that don’t depend on the template parameters, plus the name of the template itself and names declared within it.

When the compiler tries to resolve some name in the code, it first decides whether the name is dependent or not, and the resolution process stems from this distinction. While non-dependent names are resolved "normally" – when the template is defined, the resolution for dependent names happens at the point of the template’s instantiation.

When you are using this->, the compiler will delay the name lookup to the moment when the template is actually instantiated: at that point, the function calls to members of the base class will be correctly resolved.

  评论这张
 
阅读(48)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017