Never redefine an inherited default parameter value, because default parameter values are statically bound, while virtual functions — the only functions you should be overriding — are dynamically bound.
1#include<iostream>
2#include<string>
3usingnamespacestd;
4
5classShape
6{
7public:
8virtualvoiddraw(conststrings="Red")const
9{
10cout<<s<<"in shape"<<endl;
11}
12};
13
14classCircle :publicShape
15{
16public:
17virtualvoiddraw(conststrings="Blue")const
18{
19cout<<s<<"in circle"<<endl;
20}
21};
22
23intmain()
24{
25Shape*s=newCircle();
26s->draw();
27cin.get();
28return0;
29}
原文链接: https://www.cnblogs.com/zhtf2014/archive/2011/04/03/2004258.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/23299
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!