C ++虚函数执行问题 [英] C++ virtual function execute problem

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

问题描述

大家好.我看到了一个C ++面试题,有几个不明白的问题,希望有人能回答我的问题,以下代码是面试题的代码,我的问题如下:

问题1:一个类对象pA被分配了一个NULL,据我所知,在编译一个类时将有一个成员空间,以便它可以正确地调用成员函数指针,但是现在正确地调用了pA-> Function程序,为什么可以正确调用pA-> Function,并且程序没有崩溃.

问题2:为什么B类成员函数将函数声明为虚函数,以相同的方式调用.现在程序崩溃.

Hello,everyone.I saw a C++ interview questions,there are several issues do not understand, I hope someone can answer my questions,the following code is the code of interview questions,my question is as follows:

problem 1:A class object pA is assigned a NULL,as far as I know,when compile an class will have a member of the space,so that it can properly call a member function pointer,but now correctly call pA->Function procedure,why can be call correctly pA->Function,and the program did not crash。

problem 2:why do class B member function Function was declared as a virtual function,call in the same way。now the program crash。

#include <cstdio>
class A
{
public:
    void Function()
    {
        printf("class A Function execute test!\n");
    }
};
class B
{
public:
    virtual void Function()
    {
        printf("class B Function execute test!\n");
    }
};
int main()
{
    A *pA=NULL;     
    pA->Function(); //why can correctly call
    B *pB=NULL;
    pB->Function(); //why crash here
    return 0;
}


推荐答案

实际上,根据您的代码,您实际上需要使用新的A(); new B();实例化(构造)该类的实例,在两种情况下均会赢没工作;甚至与OPP都不相关,因为您只是尝试重新引用NULL.



该代码实际上与VS 2008的C ++所描述的OP一样.请参阅下面的解释,在我的评论中.
抱歉,造成一些混乱.

—SA
You need to instantiate (construct) an instance of the class using new A(); new B(); actually, as per your code, in both cases it won''t work; is is not even related with OPP as you simply try to re-reference NULL.



The code actually works as OP described with C++ of VS 2008. Please see the explanation below, in my comments.
Sorry for some confusion.

—SA


如果您问这个问题,那么您对C ++陌生.

请允许我推荐一本免费书籍(分为两卷),涵盖基础知识和其他内容,并解释C ++的为什么"和如何":

http://mindview.net/Books/TICPP/ThinkingInCPP2e.html [this"的指针作为其第一个参数. 虚拟调用是另一种野兽:在运行时,将在VTABLE中搜索正确的函数.根据您的问题,我假设您对VTABLE的概念不熟悉.直到你才去参加任何工作面试.

欢迎登机,

Pablo.
If you''re asking this question, you''re ovbiously new to C++.

Allow me to recommend a free book (in two volumes), that cover the basics and beyond, and explain the ''why'' and ''how'' of C++:

http://mindview.net/Books/TICPP/ThinkingInCPP2e.html[^]

It helped me a lot a few years ago, when I was moving from C to C++.

About your question: a non-virtual member function is just a C function that receives as its first parameter a pointer to ''this''.
A virtual call is a different kind of beast: at run time the VTABLE is searched for the correct function. From your question I assume you are not familiar with the concept of VTABLE; don''t go to any job interviews until you are.

Welcome on board,

Pablo.


调用非虚拟函数时,编译器和链接器在编译/链接时已经知道该函数的地址.因此,函数调用pA-> Function确实起作用.但是,您将无法在该函数中使用A的任何成员变量,因为此指针仍指向无效地址.该函数中的printf调用不使用此指针,因此可以.我不确定C ++标准是否可以保证这种行为,但是它可以在许多C ++实现以及其他Visual C ++中使用.

第二示例p​​B-> Function调用虚拟函数.正如Pablo在上一篇文章中已经解释的那样,调用虚函数使用所谓的vtable,即具有类虚函数所有地址的表.在大多数实现中,类对象的前几个字节用作指向vtable的指针.这就是为什么该示例必须失败的原因:我们没有类对象,因此对vtable的访问失败.

我希望这清楚说明了A为什么B不起作用的原因.
When calling a non-virtual function the compiler and linker know the address of the function already at compile / link time. Hence the function call pA->Function does work. However, you will not be able to use any member variables of A in that function, because the this-pointer is still pointing to an invalid address. The printf call in that function does not use the this-pointer, so that''s ok. I am not sure, if that behavior is guaranteed by the C++ standard, but it works on many C++ implementations, within others Visual C++.

The second example pB->Function calls a virtual function. As already explained by Pablo in the previous entry, calling a virtual function makes use of the so-called vtable, a table with all the addresses of the virtual functions of a class. In most implementation the first few bytes of the class object are used as pointer to the vtable. And that is why this example must fail: We don''t have a class object and so the access to the vtable fails.

I hope that made it clear why A works and B doesn''t.


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

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