小编Mar*_*a m的帖子

具有默认参数的虚函数,奇怪的输出

这段代码的输出是15,我真的不知道为什么.我认为它x=5foo功能中使用,但我不知道为什么.谁能帮我 ?

#include <iostream>
#include <string>

using namespace std;


struct A
{
    virtual int foo(int x = 5)
    {
        return x*2;
    }
};

struct B : public A
{
    int foo(int x = 10)
    {
        return x*3;
    }
};



int main(int argc, char** argv)
{
  A* a = new B;
  cout << a->foo();
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ virtual-functions language-lawyer default-arguments

3
推荐指数
1
解决办法
46
查看次数