C++箴言:如何访问模板化基类中的名字 (4)

上一篇 / 下一篇  2008-05-04 13:52:20

第二,你可以使用一个 using declaration,如果你已经读过《C++箴言:避免覆盖通过继承得到的名字》,这应该是你很熟悉的一种解决方案。该文解释了 using declarations 如何将被隐藏的 base class names(基类名字)引入到一个 derived class(派生类)领域中。因此我们可以这样写 sendClearMsg:ITPUB个人空间N8wyuR"qX"F,e

J~3lD)^7Gl{0
templateITPUB个人空间5vg3ab Tl
class LoggingMsgSender: public MsgSender {
Py G#Y%E)i/M*BdKpO0public:ITPUB个人空间*ZUto@u Jo
 using MsgSender::sendClear; // tell compilers to assume
a:U:tv5d1R0 ... // that sendClear is in the
taSd f A0 // base classITPUB个人空间u?n Gj/F"F
 void sendClearMsg(const MsgInfo& info)ITPUB个人空间 UG'v*v&tPv
 {ITPUB个人空间(Ux0WKo$A?s_
  ...ITPUB个人空间~+B!eV)`pJ8x$K
  sendClear(info); // okay, assumes thatITPUB个人空间4G3dC}4])n%]
  ... // sendClear will be inherited
'Dj.Bb8JV.}0 }ITPUB个人空间#R6o#E;C+CS\]
 ...ITPUB个人空间yE A{s B
};

Rqa'T;]^0  (虽然 using declaration 在这里和《C++箴言:避免覆盖通过继承得到的名字》中都可以工作,但要解决的问题是不同的。这里的情形不是 base class names(基类名字)被 derived class names(派生类名字)隐藏,而是如果我们不告诉它去做,编译器就不会搜索 base class 领域。)
u)SkD)vj/L }^0
O5G p6npI^6D-Q6I"n;@0  最后一个让你的代码通过编译的办法是显式指定被调用的函数是在 base class(基类)中的:ITPUB个人空间&G|%]@:i

+qLO&E-c+j9{?j3t0
template
R?*q DDO0class LoggingMsgSender: public MsgSender {ITPUB个人空间%K#V"O'X Hkm5[;{
public:ITPUB个人空间e^9Msp ]E!EQT}
...ITPUB个人空间OP;@.eqg%js2q+{*~
void sendClearMsg(const MsgInfo& info)
;F2| ?Krw){P:K-[0{ITPUB个人空间,W.CU%O)at:g
 ...
$ZDg$b{0 MsgSender::sendClear(info); // okay, assumes that
1_/D_3\+R:K0 ... // sendClear will be
;m,E/S]_Mbp#V/g0} // inherited
.xc,m0]2}:h0ITPUB个人空间#[,_H*h R2A5T2o
...
vp%t;S0_e0T b aD F0};

TAG:

 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

日历

« 2008-10-12  
   1234
567891011
12131415161718
19202122232425
262728293031 

数据统计

  • 访问量: 54225
  • 日志数: 3708
  • 建立时间: 2008-04-12
  • 更新时间: 2008-08-15

RSS订阅

Open Toolbar