虚函数调用非虚函数 [英] Virtual function calling a non-virtual function

查看:140
本文介绍了虚函数调用非虚函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了下面的代码片段来测试我对虚拟继承的理解。显然,我仍然不能完全得到它。这是我的代码(后面是我的问题):

  #include< iostream> 
#include< vector>

using namespace std;

class Foo
{
public:
virtual void foo();
void foo2();
};

void Foo :: foo()
{
cout< 在FOO-foo 1< endl
foo2();
}

void Foo :: foo2()
{
cout< 在FOO-foo 2< endl
}

class Bar:public Foo
{
public:
void foo();
void foo2();
};

void Bar :: foo()
{
cout<< In BAR-foo 1<< endl
foo2();
}

void Bar :: foo2()
{
cout< In BAR-foo 2<< endl
}

int main()
{
Foo * f = new Foo;
f-> foo();

Foo * b = new Bar;
b-> foo();

return 0;
}

这是我的理解:



指针f指向基类Foo并且 f-> foo()调用 foo()



基本类中的指针b是一个基类,它在基类中调用 foo2基类指针,但指向派生类Bar的对象。现在,由于 foo()是一个虚函数,它调用派生类的 foo()。现在 foo()(派生类的)调用 foo2()。因为 foo2()不是一个虚函数,我期望基类 foo2()被调用。但是我看到派生类的 foo2()正在调用。



 在FOO  -  foo 1 
在FOO - foo 2
在BAR - foo 1
在FOO - foo 2

但改为:

 在FOO  -  foo 1 
在FOO - foo 2
在BAR - foo 1
在BAR - foo 2

为什么会这样?从我的理解,vtable将只有 foo(),而不是 foo2()。那么,如何调用派生类的 foo2()



这是我的第一篇文章。请原谅我,如果我违反了任何发布准则。在 Bar :: foo()中,

您正在调用 foo2()。这实际上等效于调用 this-> foo2() 的类型是 Bar ,因此这真的等效于:

  void Bar :: foo()
{
Bar * bar = this;
bar-> foo2();
}

调用在编译时解析为 Bar :: foo2 ,而不是在运行时动态调用。


I wrote the following piece of code to test my understanding of virtual inheritance. Apparently, I still don't get it fully. Here is my code (followed by my question):

#include <iostream>
#include <vector>

using namespace std;

class Foo
{
public:
    virtual void foo();
    void foo2();
};

void Foo::foo()
{
    cout << "In FOO - foo 1" << endl;
    foo2();
}

void Foo::foo2()
{
    cout << "In FOO - foo 2" << endl;
}

class Bar : public Foo
{
public:
    void foo();
    void foo2();
};

void Bar::foo()
{
    cout << "In BAR - foo 1" << endl;
    foo2();
}

void Bar::foo2()
{
    cout << "In BAR - foo 2" << endl;
}

int main()
{
    Foo* f = new Foo;
    f->foo();

    Foo* b = new Bar;
    b->foo();

    return 0;
}

This is my understanding:

The pointer f points to the base class Foo and f->foo() calls foo() in the base class which in turn calls foo2() in the base class.

The pointer b is a base-class pointer but points to an object of the derived class Bar. Now, since foo() is a virtual function, it calls foo() of the derived class. Now foo() (of the derived class) calls foo2(). Since foo2() is not a virtual function, I was expecting the base class foo2() to get called. However I see that foo2() of the derived class is getting called.

So, I was expecting this output:

In FOO - foo 1
In FOO - foo 2
In BAR - foo 1
In FOO - foo 2

but got this instead:

In FOO - foo 1
In FOO - foo 2
In BAR - foo 1
In BAR - foo 2

Why is this so? From what I understand, the vtable will have an entry only for foo() and not for foo2(). So, how is foo2() of the derived class getting called?

This is my first post. Please excuse me if I have broken any posting guidelines. Thanks in advance!

解决方案

In Bar::foo(), you are calling foo2(). This is really equivalent to calling this->foo2(). The type of this is Bar, so this is really equivalent to:

void Bar::foo()
{
    Bar *bar = this;
    bar->foo2();
}

So there's no polymorphism involved at this point; the call is resolved to Bar::foo2 at compile-time, rather than dynamically at run-time.

这篇关于虚函数调用非虚函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆