zoukankan      html  css  js  c++  java
  • Default arguments and virtual function

     

      

      Predict the output of following C++ program.

     1 #include <iostream>
     2 using namespace std;
     3  
     4 class Base
     5 {
     6 public:
     7     virtual void fun ( int x = 0 )
     8     {
     9         cout << "Base::fun(), x = " << x << endl;
    10     }
    11 };
    12  
    13 class Derived : public Base
    14 {
    15 public:
    16     virtual void fun ( int x )
    17     {
    18         cout << "Derived::fun(), x = " << x << endl;
    19     }
    20 };
    21  
    22  
    23 int main()
    24 {
    25     Derived d1;
    26     Base *bp = &d1;
    27     bp->fun();
    28     return 0;
    29 }

      Output:

      Derived::fun(), x = 0
        

      If we take a closer look at the output, we observe that fun() of derived class is called and default value of base class fun() is used.
      Default arguments do not participate in signature of functions. So signatures of fun() in base class and derived class are considered same, hence the fun() of base class is overridden. Also, the default value is used at compile time. When compiler sees that an argument is missing in a function call, it substitutes the default value given. Therefore, in the above program, value of x is substituted at compile time, and at run time derived class’s fun() is called.

     

      Now predict the output of following program.

     1 #include <iostream>
     2 using namespace std;
     3  
     4 class Base
     5 {
     6 public:
     7     virtual void fun ( int x = 0)
     8     {
     9         cout << "Base::fun(), x = " << x << endl;
    10     }
    11 };
    12  
    13 class Derived : public Base
    14 {
    15 public:
    16     virtual void fun ( int x = 10 ) // NOTE THIS CHANGE
    17     {
    18         cout << "Derived::fun(), x = " << x << endl;
    19     }
    20 };
    21  
    22  
    23 int main()
    24 {
    25     Derived d1;
    26     Base *bp = &d1;
    27     bp->fun();
    28     return 0;
    29 }

      The output of this program is same as the previous program. The reason is same, the default value is substituted at compile time. The fun() is called on bp which is a pointer of Base type. So compiler substitutes 0 (not 10).

      In general, it is a best practice to avoid default values in virtual functions to avoid confusion.

     

     

      Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
      

      转载请注明:http://www.cnblogs.com/iloveyouforever/

      2013-11-26  20:18:49

  • 相关阅读:
    Asp.Net Web API 2第十六课——Parameter Binding in ASP.NET Web API(参数绑定)
    博客园博客评论一个奇怪的现象~~这应该不是圣诞礼包
    Asp.Net Web API 2第十五课——Model Validation(模型验证)
    PostgresQL 中有没有rownum这样的,显示结果集的序号
    在postgresqlz中查看与删除索引
    Spring事务异常rollback-only
    spring之Environment
    Spring事务管理——回滚(rollback-for)控制
    类的静态方法无法使用aop拦截
    Spring/SpringBoot定义统一异常错误码返回
  • 原文地址:https://www.cnblogs.com/iloveyouforever/p/3444049.html
Copyright © 2011-2022 走看看