调用虚函数时的奇怪行为

Cad*_*hon 5 c++ virtual

我不明白这段代码有什么问题.它看起来像一个不可思议的陷阱!

这段代码:

class Foo
{
  public:
      virtual double foo(double x) const = 0;
              double foo(int x) const { return (double)(x + x); }
};

class Bar : public Foo
{
    public:
        virtual double foo(double x) const { return x * x; }
};

int main()
{
    Bar* b = new Bar;
    Foo* f = b;
    std::cout << b->foo(3) << " " << f->foo(3) << std::endl;
    std::cout << b->foo(5.0) << " " << f->foo(5.0) << std::endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

打印以下输出:

9 6
25 25
Run Code Online (Sandbox Code Playgroud)

我推断Bar::foo(double) const当指针的类型是隐式转换时调用它Bar*.但为什么这样的事情可能没有任何警告?

我与GCC 4.7.2合作.我编译了g++ -Wall foobar.cpp -o foobar.exe

Tar*_*ama 9

这是由于名称隐藏.

声明一个名为fooin 的函数时Bar,将隐藏所有具有相同名称的声明Foo.

因此,当指针的静态类型是Bar,编译器找到Bar其中带有a 的版本double,因此它隐式转换int为满足此要求.

如果要显示int版本Foo,请添加using声明:

class Bar : public Foo
{
    public:
        using Foo::foo;
//      ^^ makes the versions in Foo visible
        virtual double foo(double x) const { return x * x; }
};
Run Code Online (Sandbox Code Playgroud)