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

hhfighting的博客

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

 
 
 

日志

 
 
 
 

C++ Inheritance: function overload/override/hide  

2014-07-15 08:30:35|  分类: C & C++ |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
Today, I met a very basic C++ problem. That's so sadC++ Inheritance: function overloading - hhfighting - hhfighting的博客.  I already do coding for many years, I still made so stupid mistake. Whatever, saying it out, give a reminder to myself.
Let's see the following programming:
  1. class A
  2. {
  3. public:
    1. A(){}
    2. void test1(){
      1. std::cout << "class A::test1" << std::endl;
    3. }
    4. void test1(int x){
      1. std::cout << "class A::test1(int):" << x << std::endl;
    5. }
    6. virtual void test2(){
      1. std::cout << "class A::test2" << std::endl;
    7. }
    8. virtual void test2(int x){
      1. std::cout << "class A::test2(int):" << x << std::endl;
    9. }
  4. };
  5. class B : public A
  6. {
  7. public:
    1. B(){}
    2. void test1(int x, int y){
      1. std::cout << "class B::test1(int,int):" << x << "-" << y << std::endl;
    3. }
    4. void test2(int x){
      1. std::cout << "class B::test2(int):" << x << std::endl;
    5. }
  8. };
  9. int main()
  10. {
    1. A* aa=new B();
    2. B* bb=new B();
    3. A* cc=new A();
    4. //aa->test1(3,4); //error: no matching function for call to ‘A::test1(int, int)
    5. bb->test1(3,4); //Output: class B::test1(int,int):3-4
    6. aa->test1(5);//Output: class A::test1(int):5
    7. //bb->test1(5); //error: no matching function for call to ‘B::test1(int)’
    8. aa->test1();//Output: class A::test1
    9. //bb->test1(); //error: no matching function for call to ‘B::test1()’
    10. aa->test2(6);//Output: class B::test2(int):6
    11. bb->test2(6);//Output: class B::test2(int):6
    12. cc->test2(6);//Output: class A::test2(int):6
    13. aa->test2();//Output: class A::test2
    14. //bb->test2();//error: no matching function for call to ‘B::test2()’
  11. }
The mistake is about function overloading. When you rewite a function in derived class, the function(s) with the same name in the base class will be hidden. Like class B, you define a new test1(*) function,  class A test1() and test1(*) will be disappearing. So in main function Line 7 and Line 9 is wrong.
Line 4 error: I misuse virtual function usage. Like in main function Line 10, test2(int) is virtual function defined in both class A and B, so which one will be called, it depends on how to initialize this variable. Like aa, even if its type is belong to class A, however it will constructed by class B, so in line 10 B::test2 will be called. Thus, cc is constructed by class A, so in line 12 A::test2 will be called.
Line 10 error: same reason with line 7 and 9.
Summary:
(1) A base class object is pointer:
If called function is a virtual function, which function will be called, base function or derived function. It depends on which construction has been used. If use base class construction, base function will be called; If use derived class construction, derived function will be called.
If called function is not a virtual function, whatever this object is constructed by base class or derived class, only base function can be called.
(2) Overload:
The  function in derived class always overload the function with the same name in base class, whatever the parameters is same or not.
Today I learned a new knowledge: (also take the above code as an example)
If you still want to use test1(int) and test1() when using class b object, you can add the following declaration:
    using A::test1;  in class B
after this, Line 7 and Line 9 will become correct, call the corresponded function in class A.
  评论这张
 
阅读(66)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

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

页脚

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